4
votes

I've created a cloud function that listens to a pubsub topic. When deployed, this creates a subscription to the correct topic and functions how I would expect it to.

export const handler = functions.pubsub.topic("topicName").onPublish(( message, context ) => {
    // code that runs
});

The deploy command used was:

firebase deploy --only functions

My question: How do I set a subscription filter for the topic, so that (for example) I only run the cloud function if an attribute is a specific value?

I know how to do this in the console by manually creating a subscription and create the filters on the subscription creation, but I can't find any documentation on how to do it in a Firebase cloud function.

1

1 Answers

3
votes

From my understanding of CF Pub/Sub triggers, once the Cloud Function gets deployed the subscription gets created as well and modifying the subscription after it has been created is not possible currently, thus I am not sure if what you want to achieve is possible, due to the back-end of Pub/Sub Cloud Functions.

I tried reproducing a potential workaround with an HTTP trigger CF instead, which would be triggered from a Subscription via Push on the Cloud Function Endpoint URL, only when the manually created filter would be fulfilled and it seems like it worked.

I followed these steps:

  • Created HTTP function to obtain the function URL
  • Create the Pub/Sub topic
  • Create Subscription type PUSH to function URL and filter attributes.domain = example.com
  • Then on the corresponding topic UI -> Publish message -> Add attribute Key = attributes.domain and Value=example.com
  • When publishing, the function gets triggered.

However, you should keep in mind that, as it is an HTTP CF, further authentication processes would be necessary to maintain security.