【发布时间】:2020-05-19 11:33:01
【问题描述】:
我需要收听大约 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中收听大量订阅的推荐方法
【问题讨论】:
标签: node.js google-cloud-platform publish-subscribe google-cloud-pubsub