setting up websockets without command line
Asked Answered
T

3

16

How can I follow a tutorial like this without using the command line?

http://www.flynsarmy.com/2012/02/php-websocket-chat-application-2-0/

I need to be able to use websockets but have a shared server and no access to the command line.

I cannot simply run the script from a browser, if the script would stop running when i closed the browser/disconnected from the internet.

Tournedos answered 15/2, 2013 at 5:47 Comment(0)
W
18

Assuming your shared hosting provider supports crontab and cron jobs (most of them do), add the following job to the crontab:

@reboot nohup php /path/to/server.php 2>&1 >/dev/null &

Additionally, you need to start it now, so simply create the following PHP file and access it once, in your browser:

<?php shell_exec('nohup php /path/to/server.php 2>&1 > /dev/null &');?>

That should do the trick. Hopefully your shared hosting provider allows execve() calls to be made. Good luck!

Warehouse answered 22/2, 2013 at 20:29 Comment(7)
will this essentially keep the script always open/accepting connections? I can run cron jobs and and run that script but it doesn't actually say 'crontab' anywhere on the page. i'm guessing that is just implied that those are 'crontab's though and that it should work?.. I can run shell_exec commands. do you mean hopefully they allow that command?Tournedos
If you use nohup to launch the server.php file, it will always keep the script open and accepting connections. Basically, the shell_exec() PHP code posted earlier would launch your socket server. The cron job was simply in order to launch the server.php file if your shared hosting server restarts.Warehouse
will the server allow the websocket in a particular port (like 6800)Brisling
@OctavO, how can we stop it ?Silicious
502 - Web server received an invalid response while acting as a gateway or proxy server. this is what error I am getting.Recension
need the same thing for windows pleaseArdor
i use codeigniter my websocket modules working while i am running command on cmd in windows, i am trying to automate thise command running process, anyone please assist me to how to do this?Raddled
F
1

Well try run Server.php on browser. Most of the time a php-cli-script also works fine on browser (Until using some command-line only functionality like argv/argc etc. ). On other tab of browser on in another browser you can run usual url like screenshot in given tutorial.

One important thing, Please check socket and other required extensions is enabled or not on your shared server.

Foiled answered 15/2, 2013 at 9:19 Comment(4)
will this essentially start my php script and keep it running on the server?... it sounds like that's what needs to happenTournedos
It will start your server script and keep running until browser tab/window closed (but only in case if server code id not dependent on some php cli-only functionality.).Foiled
Without having a script running on the server, you just can’t do it...Even if you don’t actually have access to the server, you might be able to ”break” into it by using php’s system() and that way initialize the server there. Or just go and get proper host for this purpose, I suggest.Falsehood
Or actually, when I think about it, if system() (or exec() or something similar) is available, you may be able to run ”php script.php &” in order to make it run in background, then just close the browser and it may keep alive...Falsehood
H
1

I didn't know about CRON's @reboot directive until just now, so @Octav O solution is probably much better. But while testing, I found the following code (found on Saran's websocket tutorial) quite useful.

Save this as websocket_launch.sh in the same dir as server.php ...

PID=`ps -aef | grep "server.php" | grep -v grep | awk '{print $2}'`
if [ -z $PID ]
then
    #echo "Launching now"
    nohup php server.php > error_log &
else
    #echo "Running as PID $PID"
fi

... then run the shell script with CRON every minute. The script checks if server.php is running, and if not, launches it.

Hoffer answered 22/5, 2014 at 7:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.