How to publish message in Google Pub/Sub from Firebase Cloud Function?

Augusto picture Augusto · Jan 13, 2018 · Viewed 13.7k times · Source

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

Answer

Tudormi picture Tudormi · Jan 15, 2018

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 .