Sunday, March 4, 2018

Firebase - Introduction of Cloud Functions

Back-end of any application is a heart that performs all task for the end user. Back-end executes tasks like: send push notifications, email and update other user information. It means that you no longer have to deploy your own server. You need only deploy your functions and all done. That's great for a mobile developer.

Firebase provides a functionality which is called Cloud Functions. With this new service, Firebase offers a scalable solution for running back-end code in the cloud and write a function that you want. You have to write JavaScript function and they’ll take care of setting up the server, scalability, uptime, security, etc.

Another great thing about it is that the Firebase team created an SDK for cloud functions. It means that you don’t have to write custom code to connect to your database, they provide the triggers.

The functions you write can respond to events generated by Firebase and Google Cloud features. We will call these features triggers.


Real-time Database Triggers:- With Real-time Database Triggers we’re able to create a database from Mobile in the Firebase Real-time Database. To do so we need to register for events on a specific database path like:

The functions.database.ref function is called and the database path we would like to register for is passed in as a parameter.

If you’re just using the ref function, you're registering for all events which are write, create, update and delete. If you just want to subscribe to one event you can use the following functions in addition:

onWrite():-  is activated when the data is created, destroyed, or changed.

onCreate():-  is activated when new data is created.
onUpdate():- is activated when data is updated.
onDelete():-  is activated when data is deleted.

Firebase Authentication Triggers:-  Authentication triggers allow us to execute code in response to the creation and deletion of a user account via Firebase Authentication.

According to the Firebase documentation, this trigger is invoked in the following cases:

  • A user creates an email account and password.
  • A user signs in for the first time using a federated identity provider.
  • The developer creates an account using the Firebase Admin SDK.
  • A user signs into a new anonymous auth session for the first time.
  • A Cloud Functions event is not triggered when a user signs in for the first time using a custom token.

Cloud Storage Triggers:- With Cloud Storage Triggers you can trigger the execution of a Firebase Cloud Function in response to uploading, updating, or deleting of files and folders in Google Cloud Storage. To register an event handler function we need to use the functions. storage object in the following way:
exports.storageChanges = => {...});

HTTP Triggers: These triggers can be invoked through an HTTP request and can be registered by using functions.https in the following way:
exports.httpTest = functions.https.onRequest((req, res) => {...});

As you can see the event handler functions gets two parameters: req and res. The req objects (which is a Request object from the Express framework) gives you access to the properties of the original HTTP request sent by the client. The res object can be used to send a response back to the client from the server.

Firebase Analytics Triggers:- You can understand in detail how the user interacts with your application iOS or Android app. Therefore the Analytics API exposes various events. Events of type conversion events can be used to register Cloud Functions in the following way:


Get it on Google Play

Java environment path in Windows, Linux and Mac - JAVA

We need to install Java and add JAVA_HOME in the Operating System user environment variable to work with software like Maven, Jenkins, React...