PHP NATS Client disconnects after some idle time

780 Views Asked by At

I have been using this library repejota/phpnats for developing a NATS Client that can subscribe to a particular channel. But after connecting, receiving few messages and having some 30 secs idle time, it gets disconnect itself without any interruption. However my Node.js client is working good with the same NATS server.

Here is how I am subscribing...

$c->subscribe(
    'foo',
    function ($message) {
        echo $message->getBody();
    }
);
$c->wait();

Any suggestions/help???

Thanks!

3

There are 3 best solutions below

0
Tim Havens On

Was this just the default PHP timeout killing it off? Maybe something like this:

ini_set('max_execution_time', 180); // gives about 3 minutes for example
0
Marcin Orlowski On

By default, PHP scripts can't live forever as PHP shall be rather considered stateless. This is by design and default life span is 30 seconds (hosters usually extend that to 180 secs but that's irrelevant really). You can extend that time yourself by setting max_execution_time to any value (with 0 meaning "forever") but that's not recommended unless you know you want that. If not, then commonly used approach is to make the script invoke itself (ie via GET request) often passing some params to let invoked script resume where caller finished.

0
sahindogan On
$options = new ConnectionOptions();
$options->setHost('127.0.0.1')->setPort(4222);
$client = new Connection($options);
$client->connect(-1);

You need to set connect parameters as -1