Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploying WebSockets in Apache server using PHP

I have created a bidding system which is supposed to work live and update the bidders table whenever someone bids on an item.

The current solution is not good enough to handle all the requests (I have tried long polling too, but without much success). On the clients side I am using HTML and Javascript (with AngularJS) and I send AJAX request to the server every 1 second to check for updates. (obviously a bad choice)

Recently, I tried RatchetPHP and everything worked fine locally however, I have to open a terminal and start the server.

My questions is is there any tutorial that shows how to deploy Ratchet WebSocket into a server (since I am hosting the web app somewhere). I have been searching a long time now and no one seems to clarify how the websockets are deployed in the server (Apache)

like image 602
Best Bid Avatar asked Nov 08 '15 23:11

Best Bid


2 Answers

They aren't. Since Ratchet is a PHP framework it runs in PHP. When you're serving up your webpages through the Apache httpd web server you are just using mod_php to send back the response over HTTP, but Apache httpd doesn't support web sockets. So you will have to run the web socket server in PHP itself if you're using Ratchet PHP.

Also, notice the Ratchet Documentation already shows you how to deploy this in production. The idea is to run the server in supervisord which is the simplest way to daemonize the process in a *nix environment.

When running Ratchet in production it's highly recommended launching it from suporvisord. Suporvisor is a daemon that launches other processes and ensures they stay running. If for any reason your long running Ratchet application halted the supervisor daemon would ensure it starts back up immediately.

setup
(source: socketo.me)

Notice if you're load balancing between Apache/Nginx webserver you'll need something like HAProxy as an additional reverse proxy between the Web Socket server and your regular web server.

like image 80
Sherif Avatar answered Sep 21 '22 05:09

Sherif


I wrote a great tutorial (no bias there :P) on how to start from scratch and get a ratchet web socket up and running on an ubuntu server.

http://blog.samuel.ninja/the-tutorial-for-php-websockets-that-i-wish-had-existed/

The basic idea is to make a service conf file to run the php file as a service.

like image 31
MarshallOfSound Avatar answered Sep 19 '22 05:09

MarshallOfSound