I am using ratchet for my websocket application.Below is my server code.
error_reporting(0);
date_default_timezone_set('Europe/Berlin');
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Chat;
require dirname(__DIR__) . '/vendor/autoload.php';
$docRoot = "http://myshowcam.com/NewSite";
if (!isset($conn)) {
$host = "127.0.0.1"; // Hostname
$user = "root"; // Username Here
$pass = "xxxxx"; //Password Here
$db = "myshowcam"; // Database Name
$dbh = new PDO('mysql:dbname=' . $db . ';host=' . $host, $user, $pass);
}
$server=IoServer::factory(new HttpServer(new WsServer(new Chat())), 9000, "myshowcam.com");
$server->run();
To run the server i have used the nohup command as beolow
nohup php -q ratchet/bin/chat-server.php > ratchet_ws.log &
As the process stop working after few hour i have use a shell process to check if this server is running or not and if its not running will start the server again.
while true
do
if pgrep php > /dev/null
then
echo Running
else
cd /mypath/project foldername/
nohup php -q ratchet/bin/chat-server.php > ratchet_ws.log &
fi
sleep 1;
done
Now instead of running the chat-server.php i am running this procss as below.
nohup sh chk_process.sh > check_process.log &
Now my chat-server.php running fine but its not responding after some hour.
Thank you for any help.
nohup sh
is not the best way to persist a WebSocket PHP script. You can actually run the PHP script as a system service and control it with service socket start
and service socket stop
.
You do this by making a file /etc/init/socket.conf
with the following contents
# Info
description "Runs the Chat Web Socket"
author "Your Name Here"
# Events
start on startup
stop on shutdown
# Automatically respawn
respawn
respawn limit 20 5
# Run the script!
# Note, in this example, if your PHP script (the socket) returns
# the string "ERROR", the daemon will stop itself.
script
[ $(exec /usr/bin/php -f /path/to/socket.php) = 'ERROR' ] && ( stop; exit 1; )
end script
Tutorial Reference: http://blog.samuelattard.com/the-tutorial-for-php-websockets-that-i-wish-had-existed/