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)
})
})
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.
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.
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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With