In mobile applications, if there is a piece of code which provides certain features and doesn’t need to be run on the user devices for reasons such as protecting proprietary code by concealing source code, running complex and resource intensive operations or utilizing third party services, etc., server side apps will have to created for mobile applications.

Developing, testing, deploying and maintaining server side application requires development resources and infrastructure resources. For complex server side applications, it makes sense to own teams to develop and maintain application and support infrastructure. But for simple single purpose server side functionality for mobile applications, it will be a huge investment; this is where Firebase cloud functions comes into picture.

Firebase Cloud Functions

Firebase cloud functions based on Google cloud function provide platform for creating and running server side apps for mobile applications. With Firebase cloud functions, you just create functions and deploy them without having to worry about provisioning and managing servers.

Firebase cloud functions are written in javascript or typescript and executed in node.js environment on Google cloud platform. To develop and deploy Firebase cloud functions, node.js, npm and firebase command line needs to be installed on development machine. Cloud function’s node.js module dependencies are defined in package.json file.

Cloud functions are triggered in response to events that happens on the Google cloud environment such as uploading a file to cloud storage, deleting data in database, receiving http request, etc.. Associating cloud function to a event provider and a particular event is specified in the cloud function.

Project Set up and Writing and Deploying Cloud Functions

First download and install node.js. After installing node.js, install Firebase command line by using below command.

npm install -g firebase-tools

Then login to Firebase by using below command. Log-in using your firebase account.

Firebase login

Then create firebase cloud functions project using below command.

firebase init functions

It will create project structure as shown in the below picture.

Then write functions in index.js file. Download dependencies and add them to package.json file using below command.

npm install –save node-js-moduel-name

Then deploy functions to cloud using below command.

firebase deploy — only functions[:optional function name]

Cloud Function Triggers

As mentioned before, cloud functions are run in response to events on cloud. Firebase cloud functions library exposes objects for all types of triggers which you can use to associate your function to an event provider and specific event. One function can be associated at time to one event.

To define functions which are executed in response to Firebase firestore database events, you need to use functions.firestore object and onCreate, onUpdate, onDelete and onWrite events. Please see firestore trigger cloud function and android example for details.

To define functions which are run in response to Firebase realtime database events, you need to use functions.database object and onCreate, onUpdate, onDelete and onWrite events. You can check complete realtime database trigger cloud function and android example for more information.

To define functions which execute in response to http request, you need to use functions.https object and onRequest event. Firebase cloud functions provides domain and installs SSL certificate to handle https request for http trigger cloud functions. Please see android registration and login using cloud functions and firestore example for details.

To define functions which execute in response to changes in cloud storage bucket, you need to use functions.storage object. To handle changes in default bucket, you need to use functions.storage.object() object and for specific bucket, use functions.storage.bucket(bucketname).object. For a complete android example, see android cloud functions and cloud storage example.

To define functions which execute in response to firebase authentication events such as creating and deleting users, you need to use functions.auth object.

To define functions which execute in response to Google analytics events, you need to use functions.analytics object. This is useful to send notifications in response to data in analytics event.

To define functions which execute in response to Firebase crashlytics events, you need to use functions.crashlytics.issue object and onNewDetected, onRegressed and onVelocityAlert events. To know how to implement crashlytics trigger cloud functions, see crashlytics tutorial which sends notification to support team when app crashes.