Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
})
})
like image 541
Augusto Avatar asked Jan 13 '18 14:01

Augusto


People also ask

Is firebase a pub sub?

Firebase Cloud Messaging (FCM) is primarily known for simplifying the process of sending a notification to client devices. In this post, we are going learn how to use Firebase Cloud Messaging as a push notification service and a pub/sub service in a React application.

How does Pub/Sub deliver messages to endpoints?

The Pub/Sub server sends each message as an HTTPS request to the subscriber client at a pre-configured endpoint. This request is shown as a PushRequest in the image. The endpoint acknowledges the message by returning an HTTP success status code.

Is Google Pubsub a message queue?

Google Cloud Pub/Sub is a scalable, durable event ingestion and message delivery system that allows you to create an infrastructure whose responsibility is to handle message queues. Pub/Sub delivers low-latency, durable messaging by using two core components: topics and subscriptions.


2 Answers

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 .

like image 122
Tudormi Avatar answered Sep 21 '22 03:09

Tudormi


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.
like image 29
GorvGoyl Avatar answered Sep 18 '22 03:09

GorvGoyl