Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How To Run Persitently on Ubuntu Server? #242

Open
jairus0893 opened this issue Sep 4, 2020 · 7 comments
Open

How To Run Persitently on Ubuntu Server? #242

jairus0893 opened this issue Sep 4, 2020 · 7 comments

Comments

@jairus0893
Copy link

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.

@walkor
Copy link
Owner

walkor commented Sep 4, 2020

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

@jairus0893
Copy link
Author

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

daemon1
error2

@walkor
Copy link
Owner

walkor commented Sep 4, 2020

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

@jairus0893
Copy link
Author

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 ?

@walkor
Copy link
Owner

walkor commented Sep 4, 2020

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

@jairus0893
Copy link
Author

jairus0893 commented Sep 4, 2020

use Workerman\Timer;

Thanks @walkor its working now 👍

@jairus0893 jairus0893 changed the title How Run Persitently on Ubuntu Server? How To Run Persitently on Ubuntu Server? Sep 14, 2020
@Tubusy
Copy link

Tubusy commented Sep 17, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants