How do Firebase functions handle PubSub topics created with Google Cloud?

Viewed 89

I found a two sources that indirectly reference the fact that when creating Google Cloud PubSub subscriptions with Firebase (using the firebase-functions/v1 npm package), Firebase automatically creates the PubSub Topics and PubSub subscriptions:

  1. Cap your Firebase spending #1: Setting up a billing PubSub topic (YouTube)
  2. Firebase PubSub Trigger with Message Ordering (StackOverflow)

As a result, you do not need to (for example) use $ gcloud pubsub topics create $TOPIC_NAME and $ gcloud pubsub subscriptions create $SUBSCRIPTION_NAME to create the topic and subscriptions.

Is this behavior actually documented anywhere?

I would like to create my PubSub subscriptions outside of Firebase. This enables me to create subscriptions using the "dead letter topic" feature and enables me to consistently document my Topics and Subscriptions within my larger Google Cloud Project.

Is it possible to deploy cloud functions within my Firebase project that listen to subscriptions created outside of Firebase?

For example, could I deploy Firebase cloud functions that use the @google-cloud/pubsub npm package instead of firebase-functions/v1?

1 Answers

Firebase automatically creates the PubSub Topics and PubSub subscriptions as you can see that in the process & code described in public docs.

And It is possible to deploy cloud functions within Firebase projects that listen to subscriptions created outside of Firebase. If you want to deploy cloud function which handles dead-letter-topics in firebase you have to follow these steps:

  1. Create a pubsub topic using gcloud command "gcloud pubsub topics create"
  2. Create a firebase function with trigger pointing to the topic created in step 1
  3. Deploy the function using firebase command "firebase deploy --only functions"
  4. Update the subscription created and enable dead letter feature. This could also be done using the gcloud command
  5. Make necessary updates to the firebase function and redeploy the function using firebase deploy --only functions

Reason behind updating subscription is, When deploying the function using the firebase cli, the subscription may be recreated with standard retry settings and results in disabling dead-letter-topic. So After each function deployment, you need to update the subscription accordingly.

You can use postdeploy hooks in firebase cli through which you can re-enable the dead-letter settings post a deployment automatically. Please see the below steps for the same:

  1. Create a postdeploy.sh script that contains the gcloud command to update a subscription as below. Please see below as just an example and should be modified for variables. Command details are available here
gcloud pubsub subscriptions update projects/$GCLOUD_PROJECT/subscriptions/$SUBSCRIPTION_NAME --dead-letter-topic=$TOPICNAME --max-delivery-attempts=5
  1. Reference the above postdeploy.sh in firebase.json file as a post hook. Example below:
{
  "functions": {
   "postdeploy": "./postdeploy.sh",
   "source": "functions"
  }
}
  1. Deploy the firebase function using regular firebase command firebase deploy --only functions:XYZ

For other alternatives go through this Article.

And you can deploy firebase functions which uses the @google-cloud/pubsub

Related