Amongst all of the exciting news at Google IO last week, one, in particular, stood out for us amongst the Firebase community, Extensions events! Here we will discuss what this means for Firebase Extensions while looking at some use cases for better understanding.
What are events? #
In a nutshell, events are pub-sub events. Built on top of the Eventarc, this allows developers to write decoupled code to listen to a specific pre-defined subscription.
This update became available in the latest
10.2.0 release of the Firebase admin SDK.
Firebase Extensions #
Ok, sounds good! So how do I use this with Firebase?
Publishing an event #
Topics are a fundamental part of the pub-sub ecosystem. Like any other implementation, these are also fully customizable in Firebase Extensions.
In this example, we are going to use the Stripe Payment Extension updates, which will allow an enhancement of the Stripe Webhook events. In this example, we are going to focus on when a product has been created.
A new series of topics have been defined in this extension, specifically
Extension configuration #
Events are the input type, this will allow a customized list of events to be defined.
To define an individual event topic:
Type is the topic name to which developers can subscribe, this guide provides an example of how to define a topic name.
ProviderId(required): Event types must contain a pre-fix of the provider as a unique definition.
Version(recommended): A unique version number.
TopicName: A unique relatable name for the event.
Description A free text explanation to easily describe the intention of the event.
Setting up a channel #
To publish an event, first ensure the Firebase admin SDK is a minimum version of
This will provide module access to the
Next, a channel is required to publish the event. The Admin SDK, will create a series of environmental variables, which will provide supporting arguments for creating a channel.
EVENTARC_CHANNEL Contains the fully qualified resource name generated from the extension configuration. This follows a
EXT_SELECTED_EVENTS Includes a list of all event topics defined in the extension YAML configuration.
Pushing an event #
Once a channel has been created, we can now send information to a specified event type.
Publish is a
promise based function than requires a
type matching a type defined in the extensions YAML configuration.
Data is the information we want to send as part of the published event, in this case, we will be forwarding the result from the relevant Stripe webhooks.
Subscribing to an event #
So now we have successfully set up the publishing event, we would now like to receive and extend our cloud functions to do something useful with it!
As part of the original setup, our Stripe extension will have already updated the relevant Firestore and Stripe references.
In this example, we are going to send a
Slack notification informing us that an event has been completed along with any relevant data!
Deploy a function using the following source.
Once our setup has been completed, simply add a new product in Stripe to receive the following update in Slack...
What next? #
Events in extensions allow unlimited possibilities. Let us know what you’ve been making or how you feel Events can make developing easier!