3

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!

h_h
  • 1,201
  • 4
  • 28
  • 47

3 Answers3

0

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
YanetP1988
  • 1,346
  • 3
  • 18
  • 43
Tim Havens
  • 41
  • 4
0

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.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
0
$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

THess
  • 1,003
  • 1
  • 13
  • 21
sahindogan
  • 13
  • 1
  • 2