What Can I Do with Cloud Functions?

Cloud Functions gives developers access to Firebase and Google Cloud events, along with scalable computing power to run code in response to those events. While it's expected that Firebase apps will use Cloud Functions in unique ways to meet their unique requirements, typical use cases might fall into these areas:

Review the use cases and examples for each category that interests you, and then proceed to our Get Started tutorial or to specific how-to guides for authentication events, analtyics events, and more.

Notify users when something interesting happens

Developers can use Cloud Functions to keep users engaged and up to date with relevant information about an app. Consider, for example, an app that allows users to follow one another's activities in the app. In such an app, a function triggered by Realtime Database writes to store new followers could create FCM (FCM) notifications to let the appropriate users know that they have gained new followers.

  1. The function triggers on writes to the Realtime Database path where followers are stored.
  2. The function sends a message via FCM.
  3. FCM sends a notification message to the user's device.

To review working code, see Send FCM notifications.

Other interesting notification use cases

Perform Realtime Database sanitization and maintenance

With Cloud Functions database event handling, you can modify the Realtime Database in response to user behavior, keeping the system up to date and clean. For example, in a chat room app built on Realtime Database, you could monitor write events and scrub inappropriate or profane text from users' messages. Here's how that could work:

  1. The function's database event handler listens for write events on a specific path, and retrieves event data containing the text of any chat messages.
  2. The function processes the text to detect and scrub any inappropriate language.
  3. The function writes the updated text back to the database.

To review working code, see the Text Moderation sample. This sample sanitizes inappropriate language as well as uppercase "shouting" in chat messages.

Other Realtime Database sanitization and maintenance use cases

Execute intensive tasks in the cloud instead of in your app

Developers can take advantage of Cloud Functions to offload to the Google cloud resource-intensive work (heavy CPU or networking) that wouldn't be practical to run on a user's device. For instance, you could write a function to listen for image uploads to Storage, download the image to the instance running the function, modify it, and upload it back to Storage. Your modifications could include resizing, cropping, or converting images. ImageMagick command line tools are provided for use with Cloud Functions to make this work easy.

  1. A function triggers when an image file is uploaded to Storage.
  2. The function downloads the image and creates a thumbnail version of it.
  3. The function writes that thumbnail location to the database, so a client app can find and use it.
  4. The function uploads the thumbnail back to Storage in a new location.

For a walkthrough of an image processing example, see Handle Storage Events.

Other examples of batch jobs in the Firebase cloud

Integrate with third-party services and APIs

Cloud Functions can help your app work better with other services by calling and exposing web APIs. For instance, an app used for collaboration on development could post GitHub commits to a workgroup chat room.

  1. An HTTPS function triggers via the GitHub webhook API when someone commits to a GitHub repo.
  2. The function sends a notification of the commit to a team Slack channel.

Other ways to integrate with third-party services and APIs

Send feedback about...

Need help? Visit our support page.