0
votes

Is long polling available on GCP PubSub JS SDK?

I want to be able to process multiple PubSub messages at once for example:

There are 1000 messages being sent to the topic every in a span of 1 minute.

Given that, for the next 10 secs, there will be 50 messages to be sent to a topic. I want my subscription to a long poll for 10 secs so that instead of processing each message. It will wait for 10 secs and potentially got all the 50messages.

AWS JS SDK has this feature, I was hoping I can do it on GCP as well.

https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/sqs-examples-enable-long-polling.html

This is an example of how it will work on AWS:

  1. The SQS queue have more than 5 messages.
  2. The listener receiveMessage will get 5 messages at once in a single receive. event
// Load the AWS SDK for Node.js
var AWS = require("aws-sdk");
// Set the region
AWS.config.update({ region: "REGION" });
// Set the AWS Region
const REGION = "us-east-1"; //e.g. "us-east-1"

// Set the parameters
const queueURL =
  "https://sqs.us-east-1.amazonaws.com/763335115465/long-polling-per-message"; // SQS_QUEUE_URL
const params = {
  AttributeNames: ["SentTimestamp"],
  MaxNumberOfMessages: 5,
  MessageAttributeNames: ["All"],
  QueueUrl: queueURL,
  WaitTimeSeconds: 20,
};

// Create SQS service object
const sqs = new AWS.SQS({
  region: REGION,
  credentials: {
    accessKeyId: "xx",
    secretAccessKey: "xxx",
  },
});

sqs.receiveMessage(params, function (err, data) {
  console.log({ err, data: JSON.stringify(data) });
  if (err) {
    console.log("Receive Error", err);
  } else if (data.Messages) {
    var deleteParams = {
      QueueUrl: queueURL,
      ReceiptHandle: data.Messages[0].ReceiptHandle,
    };
    sqs.deleteMessage(deleteParams, function (err, data) {
      if (err) {
        console.log("Delete Error", err);
      } else {
        console.log("Message Deleted", data);
      }
    });
  }
});


{
  "ResponseMetadata": { "RequestId": "25295507-c4ae-5106-a499-0d7808c163b8" },
  "Messages": [
    {
      "MessageId": "5dbd863e-2c50-49c8-9c4b-9f70e8db8d17",
      "ReceiptHandle": "asdf",
      "MD5OfBody": "78ef53e38c997c445f2fe1cc63c13139",
      "Body": "Test5",
      "Attributes": { "SentTimestamp": "1610991641728" }
    },
    {
      "MessageId": "09baf624-f2ee-4173-83ed-e74c0516a7e6",
      "ReceiptHandle": "asdf",
      "MD5OfBody": "c454552d52d55d3ef56408742887362b",
      "Body": "Test2",
      "Attributes": { "SentTimestamp": "1610991983369" }
    },
    {
      "MessageId": "1cac914f-d946-434a-87a0-974b14cc2eba",
      "ReceiptHandle": "asdf",
      "MD5OfBody": "b3f66ec1535de7702c38e94408fa4a17",
      "Body": "Test3",
      "Attributes": { "SentTimestamp": "1610991986299" }
    },
    {
      "MessageId": "95c2c8ad-fc7a-451a-b967-8ce1736a4cab",
      "ReceiptHandle": "asdf",
      "MD5OfBody": "f178860b5109214d9f3debe19a7800d3",
      "Body": "Test7",
      "Attributes": { "SentTimestamp": "1610991998129" }
    },
    {
      "MessageId": "3711fa29-9bbc-418d-a35f-7adbd7daa952",
      "ReceiptHandle": "asd",
      "MD5OfBody": "b6e30158b9d7d2dc8bb4f4123fe93c9b",
      "Body": "Test10",
      "Attributes": { "SentTimestamp": "1610992008975" }
    }
  ]
}

2

2 Answers

1
votes

The Cloud Pub/Sub JS library provides a streaming, per-message API for receiving messages from subscriptions. There isn't a way to tell the library to give you a batch of N messages, so you'll have to implement it yourself.

0
votes

Yes, you have it and it's documented here. Set the value of the timeout that you want to cancel the subscription. So that, if you want to wait 10 more seconds, add 10.000 millis!