I want to start using websockets to make a connection to a PHP socket server. This server should check at a certain interval if something has changed in the database and it should then send the changes back to the client. So when a client is connected, the connection should remain open aslong as the client (the webbrowser in this case) is on the page.
Then the server should check at a certain interval the database and send data back when needed. I know that the problem isn't the websockets (except that not all major browsers support it yet). Mainly my concern is PHP. I want to create the server in PHP, but can i keep a connection open to a PHP code for aslong as i want?
And how about the interval. Normally in a C#.NET exe application i would just create a timer. How could this be done in PHP??
If this is all possible, can i just simply run my PHP code in an Apache webserver?
Typically if you were to write a socket server in PHP, you would want to run it via a command line. The things you would want to take into consideration are:
You need access to the sockets, which typically you don't on a shared server, so you would have to go get yourself a VPS.
You would want to set up a file on your server that would automatically start your PHP script every time the server is rebooted. Typically this is done by placing a shell script that runs the PHP script in /etc/init.d and then place a symlink to the shell script in /etc/rc5.d (assuming that's your default level).
You would also most likely not want/need Apache running, unless you also plan to use this server as a file server. If you are using it solely for a socket server, youw ould want to install PHP-CLI. This will save you memory.
If you are trying to have real time interactions between users connected to your server, you may not want to involve a database at all. Your socket server will have a list of all subscribers connected to the socket server, so when one of them sends you a message, you can immediately send that message back out to all connected subscribers without storing anything in a slow database. Alternatively, you would want to keep info in memory if you can afford it.
There is a solution for sockets not being supported on all browsers called socket.io. Basically I believe it tries to set up a browser websocket if it's supported, but if it is not supported, it starts a Flash socket connection. Either way you use the same interface to interact with it.
You also may want to consider another language for creating your socket server. PHP isn't really built for this type of thing. A better language to use would be Javascript, especially if scalability is a concern. Node.js is a library that would allow you to build a socket server quite easily, and would be much more scalable.