Cloud Functions
Cloud Functions
To develop the back-end code for Flutter, we want to separate the code away from our Flutter SDK project folder entirely. Common examples of this back-end code include functions to create a document, delete it and list all available documents. These functions can be stored in the cloud. In this article, I will show you how to write cloud functions on your local machine and deploy them to the cloud.
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:
- Notify users when something interesting happens.
- Perform database sanitization and maintenance.
- Execute intensive tasks in the cloud instead of in your app.
- Integrate with third-party services and APIs.
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,
analytics events, and more. See the
eventType
API reference
for the complete list of supported event types.
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. Each time a user adds themselves as a follower of another user, a write occurs in the Realtime Database. Then this write event could trigger a function to create Firebase Cloud Messaging (FCM) notifications to let the appropriate users know that they have gained new followers.
- The function triggers on writes to the Realtime Database path where followers are stored.
- The function composes a message to send via FCM.
- FCM sends the notification message to the user's device.
To review working code, see Send FCM notifications.
Other interesting notification use cases
- Send confirmation emails to users subscribing/unsubscribing to a newsletter.
- Send a welcome email when a user completes signup.
- Send an SMS confirmation when a user creates a new account.
Perform database sanitization and maintenance
With Cloud Functions database event handling, you can modify Realtime Database or Cloud Firestore in response to user behavior, keeping the system up to date and clean. For example, in a chat room app, you could monitor write events and scrub inappropriate or profane text from users' messages. Here's how that could work:
- 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.
- The function processes the text to detect and scrub any inappropriate language.
- 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 database sanitization and maintenance use cases
- Purge a deleted user's content from Realtime Database.
- Limit the number of child nodes in a Firebase database.
- Track the number of elements in a Realtime Database list.
- Copy data from Realtime Database to Google Cloud BigQuery.
- Convert text to emoji.
- Manage computed metadata for database records.
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.
- A function triggers when an image file is uploaded to Storage.
- The function downloads the image and creates a thumbnail version of it.
- The function writes that thumbnail location to the database, so a client app can find and use it.
- The function uploads the thumbnail back to Storage in a new location.
- The app downloads the thumbnail link.
For a walkthrough of an image processing example, see Handle Storage Events.
Comments
Post a Comment