12

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)
})
})
Augusto
  • 3,825
  • 9
  • 45
  • 93

2 Answers2

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 .

Tudormi
  • 1,092
  • 7
  • 18
  • Do you know if I can use this in iOS app without Apple Push Notifications Service? – Augusto Jan 15 '18 at 13:53
  • Unfortunately, I am not aware of Apple Push Notifications Service. Given that Google's Pub/Sub can be implemented via the [APIs Services](https://cloud.google.com/pubsub/docs/reference/service_apis_overview), it should be independent of the platform, programming language... i.e. as long as an HTTP client can be implemented to perform requests to the REST API endpoints. I don't know if it's relevant, or a good analogy, but [Firebase Cloud Messaging](https://firebase.google.com/docs/cloud-messaging/ios/client) works with iOS as well... so it might be the same for Cloud Pub/Sub – Tudormi Jan 15 '18 at 14:13
  • 1
    your comment might worth a new SO post as it will treat a specifically different topic. From "how to publish in Pub/Sub from Firebase Cloud Function" to "how to integrate Apple Push Notifications Service with .... " (a guess) – Tudormi Jan 22 '18 at 11:04
  • 4
    Speaking of authentication - how to authenticate a Pub/Sub client library when it is used in a Fireabse Cloud Function? I tried to create a simple function and I receive a `PERMISSION_DENIED` error when the function attempts to publish a message. – Tim Mar 27 '19 at 17:35
2

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.
GorvGoyl
  • 42,508
  • 29
  • 229
  • 225