javascript - 如何从 Firebase Cloud Function 在 Google Pub/Sub 中发布消息?

标签 javascript publish-subscribe google-cloud-functions google-cloud-pubsub

我编写了一个函数来接收 http 请求并发送电子邮件。但是,我想接收一个 http 请求并发送一个 pub 消息。问题是文档不清楚。我该怎么做?

这是我的实际代码。

exports.weeklyEmail = functions.https.onRequest((req,res) => {
const email = '****@gmail.com'

console.log('Sending e-mail')

const mailOptions = {
    to: email,
    from: '****@alunos.utfpr.edu.br',
    subject: 'Teste',
    text: 'Conteudo do email '
}   

mailTransport.sendMail(mailOptions).then(
    () => {         
        res.send('Email sent')
    }
).catch(error => {
    res.send(error)
})
})

最佳答案

据我了解,您希望从您的 Firebase Cloud Functions 实现向 Pub/Sub 发送消息。

您可以轻松使用 Node.js Pub/Sub client library ,具有已经定义的功能,例如 publish .

或者,如果您愿意,可以构建自己的客户端,直接调用 REST API for Google Cloud Pub/Sub .还有一个 RPC reference如果它更适合您的需求。


你不需要

information like host, port, id, passwd of broker

作为提到的客户端,或者您的 REST API 请求将需要 authentication .

关于javascript - 如何从 Firebase Cloud Function 在 Google Pub/Sub 中发布消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48240813/

相关文章:

javascript - 无法在节点上执行 removeChild

javascript - 悬停后永久显示图像的颜色

javascript - 如何迭代和比较 JS 对象中的值?

nservicebus - 报告竞争消费者场景进展的方法

javascript - Android云函数获取数据

javascript - 如何在 JS 中编码 URL 并在 PHP 中解码?

html - 将 Pusher/Pubnub 与权威游戏服务器结合使用

php - redis pub/sub 在 php 中是否现实?

firebase函数:secrets:set is not a Firebase command

google-cloud-platform - GCP 数据流与云函数