4

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?

w00
  • 26,172
  • 30
  • 101
  • 147
  • websocket is a different protocol, so no you can't run it thru apache. Apparently there's https://github.com/disconnect/apache-websocket – miki Mar 16 '12 at 15:29

1 Answers1

5

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.

dqhendricks
  • 19,030
  • 11
  • 50
  • 83
  • A database is a must in my case. I'm not storing everything in a database, but just some settings. When one user changes a setting, then the other user must be notified asap. So i don't think nodejs is an option for me when i need a DB. But running the php server in a commandline sounds good enough for me. I'll have a look into that right now – w00 Mar 16 '12 at 15:55
  • node.js has built in database access with drivers for MySql and Drizzle. If other users must be notified right away, why store it in a database? Why not just send that info to the other users as soon as the one user sends you the message. Socket servers are not like web scripts. There is one instance of the script running at all time, which all connected users are using. This means users can share data inside of variables, relay each other messages instantly, etc. – dqhendricks Mar 16 '12 at 16:16