Cloud Pub/Sub Triggers

Google Cloud Pub/Sub is a globally distributed message bus that automatically scales as you need it. You can create a Cloud Function that handles Google Cloud Pub/Sub events by using functions.pubsub.

Trigger a pub/sub function

Cloud Functions supports Google Cloud Pub/Sub's Message. This event is triggered whenever a new Pub/Sub message is sent to a specific topic. You must specify the Pub/Sub topic name that you want to trigger your function, and set the event within the onPublish() event handler:

Access the pub/sub message payload

The payload for the Pub/Sub message is accessible from as a Message object. For messages with JSON in the Pub/Sub message body, the Firebase SDK for Cloud Functions has a helper property to decode the message. For example, here is a message published with a simple JSON payload:

gcloud beta pubsub topics publish new_topic '{"name":"Xenia"}'

You can access a JSON data payload like this via the json property:

Other, non-JSON payloads are contained in the Pub/Sub message as base64 encoded strings in To read a message like the following, you must decode the base64 encoded string as shown:

gcloud beta pubsub topics publish new_topic 'MyMessage'

Access message attributes

Pub/Sub message can be sent with data attributes set in the publish command. For example, you could publish a message with a name attribute:

gcloud beta pubsub topics publish new_topic --attribute name=Xenia

You can read such attributes from

Send feedback about...

Need help? Visit our support page.