0
votes

I have a cloud function which is doing a synchronous pull from a pubsub subscription. the request is configured using the returnImmediately flag set to true so that we do not wait for pubsub messages to be present in the queue.

const request = {
    subscription: formattedName,
    maxMessages: maxMessages,
    returnImmediately: true,
};
...
    client.pull(request)
...

When there are some messages pending, the request returns very fast ( 100ms ) but when there is no messages in the queue, the call hangs for about 1.5 second before returning the empty array of messages.

as I am in a cloud function, I am not sure asynchronous pull is possible.

how could I get rid of this 1.5 s delay and get the pull(request) returns immediately when there is no pending messages ? it would feel very natural that a synchronous pull() with returnImmediately set to true, returns immediately

1
Synchronous pubusb pull in Cloud Functions is not a good idea for many reasons. Instead, you should write a pubsub trigger to arrange for your code to run on each message. cloud.google.com/functions/docs/calling/pubsub - Doug Stevenson

1 Answers

1
votes

As @DougStevenson pointed, you should write a Cloud Function to be triggered when there is a new message published in your topic, you can follow this tutorial, then you'll have a very fast response without any delay.