starting a websockets server in php on shared hosting
hosting, localhost, php, websocket
Solution
Not having SSH access to your shared hosting is pretty flaky. That said...
You can use `exec` to run something on the command line from a triggered script. For instance, if you have a controller action that you call from a URL like http://mysite.com/server/start, you could embed the line:
$lastLine = exec("php server.php");
Of course, this command will not return until the command finishes, so you will never get a response from this script (unless it fails). So I would use `popen`, which will fork the process and return right away, allowing your controller to return a response.
$handle = popen("php server.php", "r");
At some point, you are probably going to want to stop this server. You can use `passthru` and `posix_kill` with a little unix CLI magic to get this done, maybe in another controller action that you call from a URL like http://mysite.com/server/stop, you could embed:
$output = passthru('ps ax | grep server\.php');
$ar = preg_split('/ /', $output);
if (in_array('/usr/bin/php', $ar)) {
$pid = (int) $ar[0];
posix_kill($pid, SIGKILL);
Problem
I have a chat application implemented in `PHP` using `WebSockets` (http://code.google.com/p/phpwebsocket/), and on my localhost when i test it i have to run the WebSockets server through a command `"php server.php"`, which starts the server on localhost and the file keeps running till i press "Ctrl + c" on my Ubuntu terminal. I mean it keeps on running the while loop, which is quite normal bcoz the server should be up to listen to the requests. Now my doubt is that i have hosted this application on a shared hosting, which does not give me `SSH` access, i mean i cannot get a terminal like interface where i used to run my command, so HOW will i run that `server.php` script to start my server now? And yes one thing that i mentioned i just need to run the script once, then the script will keep on running, and also the hosting provider allows to set up `cron jobs`. Please help, thanks in advance.