phpsocket.io
phpsocket.io copied to clipboard
How To Run Persitently on Ubuntu Server?
hi @walkor i would like to ask how to run this on a server and run it persistently? i run it on SSH then when i closed it the websocket is not running anymore.
i also run nohup php Server.php start >/dev/null 2>&1 & it runs in a day but in the next day the websocket is not working anymore.
Use php start.php start -d, the key is -d.
Thanks @walkor but when i try running that its not working anymore, look at this images

I can't tell you what's the problem. Can you give a simple example to reproduce.
I can't tell you what's the problem. Can you give a simple example to reproduce.
@walkor i notice that its not running when i send message and insert via mysqli , when i disable the insert it works? is there any restriction on the mysql ?
Do not initialize mysqli connections directly in the start file. Put the initialization of mysqli in onworker start like this.
use Workerman\Timer;
$io = new SocketIO(9120);
$io->on('workerStart', function()use($io) {
global $mysqli;
$mysqli = new mysqli("localhost", "user", "password", "database");
// This is a heartbeat for mysql. Avoid mysql connection being closed by the MySQL server when the connection is inactive for a long time
Timer::add(50, function () {
global $mysqli;
$res = $mysqli->query('select 1');
// If the connection closed then reconnect.
if (!$res) {
$mysqli = new mysqli("localhost", "user", "password", "database");
};
});
});
$io->on('connection', function($socket)use($io){
$socket->on('chat message', function($msg)use($io){
global $mysqli;
$mysqli->query(...);
});
});
use Workerman\Timer;
Thanks @walkor its working now 👍
On the subject of persistent running, can I sing the praises of pm2? It just works like a dream with phpsocket.io with lots of extra useful features. This is how I start it, and it's been rock solid. (start_io.php might need some modifications, IIRC)
module.exports = { apps : [{ script: 'start_io.php', watch: true, name: 'chat', args: 'start', autorestart: true }] };