What exactly entails setting up a PHP Websocket Server?
Asked Answered
H

1

9

I'm getting into Web Sockets now and have been successfully using the online websockets Pusher(didn't like it) and Scribble(amazing but downtime is too frequent since it's just one person running it).

I've followed this tutorial http://www.flynsarmy.com/2012/02/php-websocket-chat-application-2-0/ on my localhost and it works great!

What I wanted to ask is, how do I setup the server.php from the above file to run as a websocket server on an online webhost/shared server?

Or do I need to get a VPS (and if so, which one do you recommend and how can I setup the websocket server there as I've never really used a VPS before!)

Thank you very much for reading my question and answering. I've read all other question/answers here regarding sockets but haven't been able to find the answer to my above questions yet. Hopefully I find it here!

Howe answered 15/6, 2013 at 9:7 Comment(0)
S
5

This is tricky.

You need to execute the server.php script and it needs to never exit. If you have an SSH access to your shared server, you could execute it just like they do on the screenshot and make it run as a background task using something like nohup:

$ nohup php server.php
nohup: ignoring input and appending output to `nohup.out'

After invoking this (using the SSH connection), you may exit and the process will continue running. Everything the script prints will be stored into nohup.out, which you can read at any time.

If you don't have an SSH access, and the only way to actually execute a PHP script is through Apache as the result of a page request, then you could simply go to that page using a browser and never close the browser. But there will be a time out one day or another and the connection between you and Apache will close, effectively stopping the server.php script execution.

And in those previous cases, a lot of shared hosts will not permit a script to run indefinitely. You will notice that there's this line in server.php:

set_time_limit(0);

This tells PHP that there's no time limit. If the host made PHP run in safe mode (which a lot of them do), then you cannot use set_time_limit and the time limit is probably 30 seconds or even less.

So yes, a VPS is probably your best bet. Now, I don't own one myself, and I don't know what's a good/bad price, but I'd say HostGator seems fine.

Slice answered 15/6, 2013 at 16:4 Comment(3)
Please note however that the community is not that much into PHP when a server application is concerned. If you're going to buy a VPS anyway, you should have a look at socket.io (Node.js) or Autobahn (Python). Those are integrated solutions (matching libraries for server and client sides) that have been proven to work well. Of course you can start with your minimalist PHP server to understand the protocol and "low-level" aspects.Slice
The best PHP solution I'm aware of is Ratchet. There are a number of self hosted solutions for other languages ( see realtime web tech guide). Generally for PHP I'd recommend using a hosted solution.Stavros
Thanks providing that link, leggetter. I shall look into it. Though I just want to ask, if I already have my own server-side code written (in PHP) and executing on my localhost, could I just host that on a VPS and connect to it from the client?Howe

© 2022 - 2024 — McMap. All rights reserved.