Introducing Extension Events

mhadaily

Majid Hajian

Posted on May 19, 2022

Introducing Extension Events

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

events:
  - type: com.stripe.v1.product.created
    description: Occurs whenever a product is created.

  - type: com.stripe.v1.product.deleted
    description: Occurs whenever a product is deleted.
Enter fullscreen mode Exit fullscreen mode

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.

{provider_id}.{version}.{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 10.2.0.

{
    "firebase-admin": "^10.2.0",
}
Enter fullscreen mode Exit fullscreen mode

This will provide module access to the eventarc import:

import { getEventarc } from 'firebase-admin/eventarc';
Enter fullscreen mode Exit fullscreen mode

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.

const eventChannel =
  process.env.EVENTARC_CHANNEL &&
  getEventarc().channel(process.env.EVENTARC_CHANNEL, {
    allowedEventTypes: process.env.EXT_SELECTED_EVENTS,
  });
Enter fullscreen mode Exit fullscreen mode

EVENTARC_CHANNEL Contains the fully qualified resource name generated from the extension configuration. This follows a projects/{project}/locations/{location}/channels/{channel-id} format.

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.

await eventChannel?.publish({
  type: `com.stripe.v1.${event.type}`,
  data: event.data.object,
 });
Enter fullscreen mode Exit fullscreen mode

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...

slack-message-hook

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!

You may follow us on Twitter,Linkedin, and Youtube or sign-up for our newsletter to get our latest updates.

PS: This post was initially written by Darren Ackers, the lead developer at invertase.io. You may find original content here

💖 💪 🙅 🚩
mhadaily
Majid Hajian

Posted on May 19, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related