phpsocket.io icon indicating copy to clipboard operation
phpsocket.io copied to clipboard

How To Run Persitently on Ubuntu Server?

Open jairus0893 opened this issue 5 years ago • 7 comments

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.

jairus0893 avatar Sep 04 '20 07:09 jairus0893

Use php start.php start -d, the key is  -d.

walkor avatar Sep 04 '20 08:09 walkor

Thanks @walkor but when i try running that its not working anymore, look at this images

daemon1 error2

jairus0893 avatar Sep 04 '20 09:09 jairus0893

I can't tell you what's the problem. Can you give a simple example to reproduce.

walkor avatar Sep 04 '20 09:09 walkor

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 ?

jairus0893 avatar Sep 04 '20 12:09 jairus0893

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(...);
  });
});

walkor avatar Sep 04 '20 12:09 walkor

use Workerman\Timer;

Thanks @walkor its working now 👍

jairus0893 avatar Sep 04 '20 16:09 jairus0893

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 }] };

Tubusy avatar Sep 17 '20 12:09 Tubusy