Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Websocket server with PHP

I'm currently developing a PHP application that is going to use websockets for client-server communication. I've heard numerous times that PHP shouldn't be used for server applications because of the lack of threading mechanisms, its memory-management (cyclic references) or the unhandy socket library.

So far, everything is working quite well. I'm using phpws as the websocket library and the Doctrine DBAL to access different database systems; PHP is version 5.3.8 . The server should serve a maximum of 30 clients. Yet especially in the last days I've read several articles stating the ineffectiveness of PHP for long running applications.

Now I'm not aware whether I should continue using websockets with PHP or rebuild the entire serverside application. I've tried Python with Socket.IO, though I did not get the results I expected.

I guess I have the following options:

  • Keep everything as it is.
  • Make the application use Ajax in combination with Socket.IO - e.g. run a serverside script that invokes the client's ajax calls when data is submitted to the server.

The last point sounds quite interesting, though it would require some work .. Would it be a problem for servers to execute all the clients requests at one time?

What would you recommend? Is the problem with PHP's memory management (I'm using gc_collect each time a client sends data to the server) still valid? Are there other reasons beside the obvious reasons (no threading, ...) for not using PHP as a server?

like image 853
lukew Avatar asked Oct 06 '22 21:10

lukew


1 Answers

You can try running your socket.io on a node server on another port on your server (that is if you are not using a hosting plan like goDaddy).

I am using it and the performances are really satisfying. I have an apache server on the port 80 serving my php files, and my server-client communications are done using a Node.js server running socket.io on the port 8080 (dev) or 843 (prod).

Node.js is really light and has great performance, but you need to run it as a server. Nodejitsu.com is a hosting solution that has the websocket protocol available and is on beta, so it is still free for now. Just note that you need to listen on the port 80 with socket.io, this is a limitation from theyr network.

If you want your pages all to be accessed on the port 80 then you will need a reverse proxy like varnish .

I hope that helps! Have a nice day.

like image 153
Bruno Gagnon-Adam Avatar answered Oct 10 '22 03:10

Bruno Gagnon-Adam