Join us in person and online for Firebase Summit on October 18, 2022. Learn how Firebase can help you accelerate app development, release your app with confidence, and scale with ease. Register now

Remote Config triggers

Stay organized with collections Save and categorize content based on your preferences.

You can trigger a function in response to Firebase Remote Config events, including the publication of a new config version or the rollback to an older version. This guide describes how to create a Remote Config background function that performs a diff of two template versions.

Trigger a Remote Config function

To trigger a Remote Config function, use the firebase-functions/v2/remoteConfig subpackage. To respond to template updates in the manner shown in this guide, you'd use the onConfigUpdated function, importing it along with other required components such as the Firebase Admin SDK:

// The Cloud Functions for Firebase SDK to create v2 Cloud Functions and set up triggers.
const { onConfigUpdated } = require('firebase-functions/v2/remoteConfig');
// The Firebase Admin SDK to obtain access tokens.
const admin = require('firebase-admin');
admin.initializeApp();
const rp = require('request-promise');
const jsonDiff = require('json-diff');

The TemplateVersion object returned by onConfigUpdated contains the key metadata fields for a template update such as the version number and time of the update. You can also retrieve the email for the user who made the update, with name and an image if available.

Here's an example of a Remote Config function that returns a diff of each updated version and the version it replaced. The function examines the versionNumber field of the template object and retrieves the current (newly updated) version together with the version one number lower:

exports.showconfigdiff = onConfigUpdated((event) => {
  // Obtain the access token from the admin SDK
  return admin.credential.applicationDefault().getAccessToken()
    .then(accessTokenObj => {
      return accessTokenObj.access_token;
    })
    .then(accessToken => {
      // Get the version number from the event object
      const currentVersion = event.data.versionNumber;
      const templatePromises = [];
      templatePromises.push(getTemplate(currentVersion, accessToken));
      templatePromises.push(getTemplate(currentVersion - 1, accessToken));
      // Get the templates
      return Promise.all(templatePromises);
    })
    .then(results => {
      const currentTemplate = results[0];
      const previousTemplate = results[1];
      // Figure out the differences of the templates
      const diff = jsonDiff.diffString(previousTemplate, currentTemplate);
      // Log the difference
      functions.logger.log(diff);

      return null;
    }).catch(error => {
      functions.logger.error(error);
      return null;
    });
});

This sample uses the json-diff and request-promise modules to create the diff and build the request to get the template object. For a 1st-generation function sample that incorporates Remote Config client logic as well as Firebase Cloud Messaging, see Propagate Remote Config updates in real time.