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:
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:
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.
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.
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.
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.
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.