1x2x3x4x
1x2x3x4x

Reputation: 604

Pull messages from pubsub without timeout

I'm trying to implement this and so far for every example Google has a timeout function that stops listenting for messages after 1 minute.

  setTimeout(() => {
    subscription.removeListener('message', messageHandler);
    console.log(`${messageCount} message(s) received.`);
  }, timeout * 1000)

Let's say I want to listen forever for new messages. What's the downside of not having the timeout function? Or would rather be better to just loop infinitely?

I'm testing locally and that's ok, but I'm wondering about what could happen in a production mode. And yes, I want to PULL messages.

Upvotes: 0

Views: 1284

Answers (1)

guillaume blaquiere
guillaume blaquiere

Reputation: 76000

The timeout is set to remove the message listener. If you want to listen indefinitively, remove these 4 lines, and that's all!

However, ask yourselves about "How to stop listening gracefully?" In case of compute engine preemption, app or VM stop, unload instances (for Cloud Run, Cloud Functions and App Engine).

Here the trigger it's a timeout. You can imagine a while loop and check a boolean to continue or to stop.

Upvotes: 1

Related Questions