basis-company / nats.php

nats jetstream client for php
116 stars 29 forks source link

How to make subscribers live listen to stream messages ? #46

Closed franck-grenier closed 9 months ago

franck-grenier commented 9 months ago

Hello, I'm setting up a SNS/SQS like fan-out queuing system with NATS.

I setup my stream with its subject and two durable pull consumers.

My publisher is working and is pushing messages into my stream. This part is ok.

My 2 subscriber scripts connect to each consumer and get the messages of the stream when they start. This part is ok too.

// subscriber 1
$stream = $client
    ->getApi()
    ->getStream('mystream');

$consumer = $stream->getConsumer('consumer1');

$consumer->handle(function ($message) {
    printf("Data: %s\r\n", $message);
});

The problem is : after receiving messages at start, the subscribers do not receive live new messages pushed by the publisher. I need to restart them to have them receive the new messages. I want them to listen forever.

I had a look around iterations but unsuccessfully... What am I missing ?

Thanks for your help

franck-grenier commented 9 months ago

Ok, I found it.

$consumer = $stream->getConsumer('consumer1')->setExpires(0);

The consumer keeps listening to new messages forever.