Catch up on everything announced at Firebase Summit, and learn how Firebase can help you accelerate app development and run your app with confidence. Learn More

Remote Config triggers

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 set up triggers and logging.
const {onConfigUpdated} = require("firebase-functions/v2/remoteConfig");
const logger = require("firebase-functions/logger");
// The Firebase Admin SDK to obtain access tokens.
const admin = require("firebase-admin");
admin.initializeApp();
const fetch = require("node-fetch");
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
        logger.log(diff);

        return null;
      }).catch((error) => {
        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.