Javatpoint Logo
Javatpoint Logo

Firebase Cloud Function

Firebase Cloud Function

Mobile and web applications require back-end code to execute tasks like sending out notification or processing long-running tasks. In the past decade, this back-end code was running on a server.

Recently Google's Firebase has introduced a new service named Cloud Functions. With the help of Cloud Functions, Firebase provides a scalable solution for running the back-end code in the cloud.

Cloud functions for firebase enable us to automatically execute the backend code in response to events triggered or caused through HTTPS requests and Firebase features. Our code is stored in Google's cloud and runs in a managed environment and no need to manage and scale our own server.

There are various advantages of running code in the cloud, which are as follows:

  1. There is no need to run and maintain our own server.
  2. For back-end code, we do have an isolated code base.
  3. We only get the bill for the actual executing time of our code.
  4. The infrastructure of the cloud is highly scalable.

Cloud Functions allows developers access to Google Cloud events and Firebase, along with scalable computing power for running the code in response to those events. It is expected that Firebase applications will use Cloud Functions in unique ways to meet their unique and specific needs, use cases may fall into the following areas:

  1. Notifying users when something interesting happens.
  2. Performing Realtime Database maintenance and sanitization.
  3. Executing intensive tasks in the cloud rather than executing in our application.
  4. Integrate with APIs and third-party services.

Notifying users when something interesting happens

Cloud Function is used to keep users engaged and up to date with relevant information about an application. For example, consider an app which allows users to follow each other's activities in the app. In such an app, a function which is triggered by the Realtime database writes to store new followers. These followers can create Firebase Cloud Messaging to make the appropriate users who know that they have gained new followers.

\

Firebase Cloud Function
  1. This function writes to the Realtime database path where the followers are stored.
  2. A message is composed by the function to send via FCM.
  3. A notification message is sent to the user's device via FCM.

Performing Realtime Database maintenance and sanitization

By using Cloud Functions database event handling, we can keep the system clean and up to date, and modify the realtime database in response to user behavior. For example, in a chat room application which is built on a realtime database, we can track writing events and clear inappropriate or profane text from users' messages.

Firebase Cloud Function
  1. The function's database event handler listens to a write event on a specific path and retrieves event data which contains the text of any chat messages.
  2. To detect and clear any inappropriate language, the function processes the text.
  3. On the back of the database, the function writes the updated text.

Executing intensive tasks in the cloud rather than in our application

Users can take advantage of Cloud Functions to take off to the Google Cloud resource-intensive tasks (heavy CPU or networking), which is practically impossible to run on user's device.

For example, we write a function to listen for image upload to the storage, running the function, download the image to the instance, upload it, and modify it back to storage. Our modifications may include cropping, resizing images, etc. For making this work easier, ImageMagick command-line tools are given to use with Cloud Functions.

Firebase Cloud Function
  1. When an image file is uploaded into cloud storage, a function triggers.
  2. The function creates a thumbnail version of the image and downloads the image.
  3. The thumbnail location writes to the database through so that a client app can find it or use it.
  4. The function uploads this thumbnail back to storage in a new location.
  5. The application downloads the thumbnail link.

Integrate with third-party services and APIs

Cloud Functions can help our app work well with other services by calling and exposing web APIs. For example, an app is used to collaborate on development which can send GitHub to a workgroup chat room.

Firebase Cloud Function
  • The user pushes commit to a GitHub repo.
  • An HTTPS function triggers through the GitHub webhook API.
  • The function notifies for the commit to a team Slack channel

How does it work?

After writing and deploying a function, Google's servers begin to manage the function immediately. We can fire the function directly with an HTTP request, or, in the case of background functions. When the function is triggered, Google's servers will listen for the events and execute that respective function.

Google responds via rapidly scaling the number of virtual server instances needed to run our function when the load increases or decreases. Each function runs separately, in its own environment with its own configuration.

The lifecycle of a background function

The developer writes code for the new function, defining the conditions under which the function should execute, and selecting an event provider (such as Realtime Database).

The developer will deploy that function, which gets connected to the selected event provider by the firebase.

The code is invoked when the event provider generates an event which matches the function's conditions.

Google creates more instances to handle work quickly if the function is busy in handling many events; if the function is idle, instances are cleaned up.

All instances for the old version are cleared and replaced via new instances when the developer updates the function by deploying updated code.

The connection between the event provider and the function is removed, and all instances are cleaned up when a developer deletes the function.


Next TopicFirebase Triggers




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA