Firebase Triggers
The Functions which we write can respond to events that are generated by Firebase and Google Cloud features called triggers. Before developing our first Cloud Function, we explore the most common triggers which we can use for Cloud Function. There are the following triggers:
Cloud Firestore Triggers
The Cloud Functions for Firebase SDK exports a function.firestore object, which allows us to create handlers tied to specific Cloud Firestore events. One thing to be noted is that Cloud Firestore events will trigger only on document changes. Adding events to specific fields is not possible.
S.No | Event Type | Trigger |
---|---|---|
1. | onCreate | It is triggered when a document is written for the first time. |
2. | onUpdate | It is triggered when a document already exists and has any value changed. |
3. | onDelete | It is triggered when a document is deleted with the data. |
4. | onWrite | It is triggered when onCreate, onDelete, or onUpdate is triggered. |
Realtime Database Triggers
By using Realtime Database triggers, we can respond to changes in the Firebase Realtime Database. To do this, we have to register for events on a specific database path function.database.ref (‘/foo/bar’). The database path which we want to register is passed as a parameter.
With curly braces, it is also possible to define this part of the path as a wildcard.
S.No | Event Type | Trigger |
---|---|---|
1. | onWrite() | It is activated when the data is created, changed, or destroyed. |
2. | onCreate() | It is activated when new data is created. |
3. | onUpdate() | It is activated when data is updated. |
4. | onDelete() | It is activated when data is deleted. |
Authentication Triggers
By using the authentication triggers, we can execute code in response to the creation and deletion of a user account via Firebase Authentication. The exports.newUserCreated = functions.auth.user().onCreate(event => { … }) is used to create an event handler function which is executed if a new user is created.
The Authentication trigger is invoked in the following cases:
- When a user creates an email account and password.
- When a user signin for the first time with the help of a federated identity provider.
- When the developer creates an account by using the Firebase Admin SDK.
- For the first time when a user signs in to a new anonymous auth session.
A Cloud Functions event is not triggered if a user signs in for the first time using a custom token.
Cloud Storage Triggers
We can trigger a function in response to the updating, uploading, and deleting of files and folders in Cloud Storage. For registering an event handler function, we have to use the function.storage object in the following way:
In the above line of code, we are registering an event handler for all object changes on the default storage bucket. If we want to specify a specific storage bucket, we have to add the call of the bucket function as well:
Within the event handler function, we can make use of various storage attributes:
S.No | Attribute | Description |
---|---|---|
1. | event.data | The storage object. |
2. | event.data.bucket | The storage bucket that contains the file. |
3. | event.data.name | The path of the file in the bucket. |
4. | event.data.contentType | The file content type. |
5. | event.data.resourceState | Either exist or not_exists. The not_exists value is set if the file/folder has been deleted. |
6. | event.data.metageneration | It is the no. of times the metadata of the file has been generated, and for new objects, the initial value is 1. |
HTTP Triggers
HTTP trigger can be used with Firebase Cloud Function. These triggers are invoked through an HTTP request and registered by using functions.https in the following way:
Remote Config Triggers
In response to Firebase remote config events, we can trigger a function, including publishing a new configuration version or rollback to an older version. For triggering a Remote Config function, we use the onUpdate handler, which is provided by function.remoteConfig.
The onUpdate returns the TemplateVersion object, which contains the key metadata fields for a template update such as time and version number of the update. We can also retrieve the email for the user who had made the update, with an image and name, if available.
Analytics Triggers
By using Google Analytics for Firebase, we can understand in detail how the user interacts with our Android app and iOS app. The Analytics API exposes various events. Type conversion events can be used to register cloud functions as follows:
Crashlytics Triggers
In response to Crashlytics issue events, which include new issues, velocity alerts, and regressed issues, we can trigger a function.
For triggering a Crashlytics function, we generate an IssueBuilder with functions.crashlytics.issue(), and after that, we call the builder’s appropriate issue generation function.
S.No | Function | Description |
---|---|---|
1. | onNew() | This is triggered when our app experiences an issue for the first time. exports.sendOnNewIssue = functions.crashlytics.issue().onNew(async (issue) => { // ... }); |
2. | onRegressed() | It is triggered when an issue reoccurs after it is closed in Crashlytics. exports.sendOnRegressedIssue = functions.crashlytics.issue().onRegressed(async (issue) => { // ... }); |
3. | onVelocityAlert() | It is triggered when a statistically significant number of sessions in a given build crash. exports.sendOnVelocityAlert = functions.crashlytics.issue().onVelocityAlert(async (issue) => { // ... }); |
Pub/Sub Triggers
Google Cloud Pub/Sub is a globally distributed message bus which automatically scales as we need it. We can create a function which handles Google Cloud Pub/Sub events by using functions.pubsub.
We can trigger a function when a new Pub/Sub message is sent to a specific topic. We have to specify the Pub/Sub topic name for triggering our function and set the event within the onPublish() event handler.
Test Lab Triggers
We can trigger a function in response to the completion of a test matrix in Firebase Test Lab. For creating a new function which triggers when a TestMatrix completes with the event handler function.testLab.testMatrix().onComplete():