在 nodejs 中创建大量 google pubsub 订阅者的推荐方法是什么?
What's the recommended way of creating a large no of google pubsub subscribers in nodejs?
我需要收听大约 40 个订阅(未来几天不会增加)。
如果那时只有一个订阅,我会做以下事情,
const subName = 'mysubscription'
const pubSubClient = new PubSub()
const startVidsubscription = pubSubClient.subscription(subName)
startVidsubscription.on('message', messageHandler)
const messageHandler = async(message) => {
// do stuff
}
但是,当我有几十个订阅时,我不能这样做。
所以,我正在尝试如下所示:
// 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)
}
但这给了我像 Unexpected token, expected ;
这样的错误。
有没有大佬告诉我在nodejs中收听大量订阅的推荐方法
您的一般方法应该有效。您可能 运行 在将订阅对象分配给 subscription.variable
时遇到问题,该对象已定义为字符串。
像下面这样的东西会起作用:
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);
}
然后您可以使用 subscriptionDetails
中的 variable
字段引用订阅对象。
如果您想通过名称 'M10057Subscription' 引用订阅对象,您可以创建订阅对象映射:
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
我需要收听大约 40 个订阅(未来几天不会增加)。
如果那时只有一个订阅,我会做以下事情,
const subName = 'mysubscription'
const pubSubClient = new PubSub()
const startVidsubscription = pubSubClient.subscription(subName)
startVidsubscription.on('message', messageHandler)
const messageHandler = async(message) => {
// do stuff
}
但是,当我有几十个订阅时,我不能这样做。 所以,我正在尝试如下所示:
// 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)
}
但这给了我像 Unexpected token, expected ;
这样的错误。
有没有大佬告诉我在nodejs中收听大量订阅的推荐方法
您的一般方法应该有效。您可能 运行 在将订阅对象分配给 subscription.variable
时遇到问题,该对象已定义为字符串。
像下面这样的东西会起作用:
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);
}
然后您可以使用 subscriptionDetails
中的 variable
字段引用订阅对象。
如果您想通过名称 'M10057Subscription' 引用订阅对象,您可以创建订阅对象映射:
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