最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to publish message in Google PubSub from Firebase Cloud Function? - Stack Overflow

programmeradmin2浏览0评论

I wrote a function that receive a http request and send a e-mail. But, I would like receive a http request and send a pub message. The problem is that the documentation is not clear. How I do that?

This is my actual code.

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

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)
})
})

I wrote a function that receive a http request and send a e-mail. But, I would like receive a http request and send a pub message. The problem is that the documentation is not clear. How I do that?

This is my actual code.

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)
})
})
Share Improve this question edited Aug 26, 2019 at 11:56 Augusto asked Jan 13, 2018 at 14:30 AugustoAugusto 4,23310 gold badges51 silver badges110 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 15

As I understand, you wish to send a message to Pub/Sub from your Firebase Cloud Functions implementation.

You can easily use the Node.js Pub/Sub client library, with already defined functionalities, like publish.

Or, if you prefer, you can build your own client, directly calling the REST API for Google Cloud Pub/Sub. There's also a RPC reference if it suits your needs better.


You won't need

information like host, port, id, passwd of broker

as the mentioned client, or your REST API requests will require authentication .

You need to import pub-sub library const {PubSub} = require('@google-cloud/pubsub'), initiate client, and publish messages.
Docs:

  • https://cloud.google.com/pubsub/docs/publisher#publish-messages-to-a-topic
  • https://cloud.google.com/functions/docs/calling/pubsub

However, One more way is to trigger background function on firestore writes.

whenever you create a doc in a collection (pubsub), your bg function will get invoked on the doc write:

exports.myTriggerFunction = functions.firestore
  .document('pubsub/{docId}')
  .onWrite((change, context) => { /* ... */ });

Also, it's just not limited to onCreate:

Event Type  Trigger
onCreate    Triggered when a document is written to for the first time.
onUpdate    Triggered when a document already exists and has any value changed.
onDelete    Triggered when a document with data is deleted.
onWrite     Triggered when onCreate, onUpdate or onDelete is triggered.
发布评论

评论列表(0)

  1. 暂无评论