farhan
farhan

Reputation: 79

What's the recommended way of creating a large no of google pubsub subscribers in nodejs?

I have around 40 subscriptions (this no will increase in the coming days) that I need to listen to.

If there was just one subscription then, I would have just done the following,

const subName = 'mysubscription'
const pubSubClient = new PubSub()
const startVidsubscription = pubSubClient.subscription(subName)
startVidsubscription.on('message', messageHandler)

const messageHandler = async(message) => {
// do stuff

}

But, I cannot do this when I have tens of subscriptions. So, I was trying something like below:

// the name is the name of each subscription and the variable is the variable name in which I will be holding the subscription object. 
var subscriptionDetails = [
    {'name': 'M10057-sub-cam1Api', 'variable': 'M10057Subscription'},
    {'name': 'M10058-sub-cam1Api', 'variable': 'M10058Subscription'},
    {'name': 'M10059-sub-cam1Api', 'variable': 'M10059Subscription'},
]

for(const subscription of subscriptionDetails){
        var subscription.variable = pubSubClient.subscription(subscription.name)
        subscription.variable.on('message', messageHandler)
    }

But this is giving me errors like Unexpected token, expected ;.

Anyone please let me know the recommended method of listening to a large no of subscriptions in nodejs

Upvotes: 0

Views: 70

Answers (1)

Lauren
Lauren

Reputation: 1009

Your general approach should work. You're likely running into problems assigning the subscription object to subscription.variable, which is already defined as a string.

Something like the following would work:

const {PubSub} = require('@google-cloud/pubsub');

const pubSubClient = new PubSub();
const messageHandler = async(message) => {
  // ... handle message ...
};

var subscriptionDetails = [
  {'name': 'M10057-sub-cam1Api'},
  {'name': 'M10058-sub-cam1Api'},
  {'name': 'M10059-sub-cam1Api'},
];

for (const subscription of subscriptionDetails) {
  subscription.variable = pubSubClient.subscription(subscription.name);
  subscription.variable.on('message', messageHandler);
}

You could then reference the subscription objects using the variable fields in subscriptionDetails.

If you wanted to reference a subscription object by the name 'M10057Subscription', you could create a map of subscription objects:

var subscriptionDetails = [
  {'name': 'M10057-sub-cam1Api', 'variable': 'M10057Subscription'},
  {'name': 'M10058-sub-cam1Api', 'variable': 'M10058Subscription'},
  {'name': 'M10059-sub-cam1Api', 'variable': 'M10059Subscription'},
];

var subscriptionObjects = {};

for (const subscription of subscriptionDetails) {
  subscriptionObjects[subscription.variable] = pubSubClient.subscription(subscription.name);
  subscriptionObjects[subscription.variable].on('message', messageHandler);
}

// subscriptionObjects['M10057Subscription'] is a subscription object

Upvotes: 1

Related Questions