Firebase Cloud Functions : Introduction to triggers all you ever wanted

I had been questioning myself since I started using Google Firebase, that If I could manage to hook some remotely written functions to Firebase Products, like Realtime Database, Storage, etc. and trigger them on different events.

This requirement was faced firstly with Firebase Realtime Database and Firebase Authentication.

Consider below two cases:

Case 1: You have “Users” object lying in your Firebase Database as below:

And You want a Notification when there is any Write operation on this “Users” key, lets say, if an object gets inserted in “Users” key, one should get Notification.

WorkAround: One solution, I found on stackoverflow, was to keep Database Reference Live in Service and listen for changes by using onChildXXX() method, and generate Notification from there. According to me, this is not a good and efficient solution.

Case 2: You want to get an Email when any new User registers in your app.

What if you are provided with a way to write such callbacks on currently trending backend platform Node.js

You would be glad to welcome this new Product in Firebase family, Cloud Functions. Lets not beat around the bush, and lets get familiar with Cloud Functions.

What are Cloud Functions ?

From the documentation:

  • “…lets you create functions that are triggered by Firebase products, such as changes to data in the Realtime Database, uploads to Cloud Storage, new user sign ups via Authentication, and conversion events in Analytics.”
  • So, there lies Event Providers (also known as Triggers) who provides:
  1. Event: to write Cloud Functions to listen for
  2. Callback: to handle a callback for any event that has happened
  • For now, We are got Event Providers (Triggers) for:
  1. Realtime Database Triggers
  2. Firebase Authentication Triggers
  3. Firebase Analytics Triggers
  4. Cloud Storage Triggers
  5. Cloud Pub/Sub Triggers
  6. HTTP Triggers

Things you can achieve

Well, you achieve a lot more tasks using Cloud Functions, you can just think like:

  1. You can send Notification when User has a new Follower.
  2. Send Confirmation / Welcome Emails when some new User joins your app.
  3. Send verification SMS to a User
  4. Creating thumbnail version of the image uploaded to Firebase Storage
  5. Clean all the User’s content from Firebase Database and Firebase Storage, when User Deletes its account from Firebase Authentication

And what not…!!!

Firebase’s Repository Function Samples provides bunch of examples for you to try out.

“But.. But.. How would I be able to ? I am Android Developer (or iPhone Developer), and Node Scripts are written in JavaScript..”

Well, answer to your question is, I am too a Native Mobile Application Developer (for Android). My JavaScript skill level is Beginner’s. But when I looked at the Syntax, and tried writing it, I too became familiar with JavaScript and I am now able to write any Scripts in Node.js

Believe me, its really easy to interpret what the syntax is trying to do. Let me, help you with a code snippet I have below:

var functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
functions.auth.user().onCreate(event => {

const user = event.data;

var userObject = {
displayName : user.displayName,
email : user.email,
photoUrl : user.photoURL,
createdOn : user.metadata.createdAt
};
admin.database().ref('users/' + user.uid).set(userObject);
});

I promise I won’t go deep with concepts but merely try to help you visualize the syntax. Lets start.

  1. Try to visualize below two lines and what you observe:
var functions = require('firebase-functions');
const admin = require('firebase-admin');

You’re writing Firebase Cloud Functions, so the first line seems to be initializing this “firebase-functions”. Similarly, second line also looks like initializing some kind of admin features.

2. Then, try the next line:

admin.initializeApp(functions.config().firebase);

const admin needs to initialize something before we start writing our actual function.

3. Finally, this is the important one:

functions.auth.user().onCreate(event => {

const user = event.data;
var userObject = {
displayName : user.displayName,
email : user.email,
photoUrl : user.photoURL,
createdOn : user.metadata.createdAt
};
admin.database().ref('users/' + user.uid).set(userObject);
});

Forget functions, what do you think this would do: auth.user().onCreate() it will give some kind of callback when a new User gets created, or something similar to it. Whenever, you think of any callback type of mechanism, most of the time callback functions gives something in arguments related to the event that happened.

In the same way, this callback function onCreate() has event object as function argument. (Syntax looks similar to Lambdas…!!??)

Next, event.data returns user object which contains User’s UID, Email, Display Name, PhotoUrl, etc.

Then, userObject is new object created using information available from user.

database.ref(‘users/’+user.uid).set(userObject); This seems to be writing some values to Firebase Database. Yes you got it correctly. This line sets this userObject object inside “Users” object in Firebase Database.

It wasn’t really that hard, right ??

The snippet listens for creation of new User using Firebase Authentication. When any new User gets created, this callback gets called and inserts a new object in “Users” object lying in Firebase Database.

Links

Firebase Cloud Functions Samples: https://github.com/firebase/functions-samples

My next blog on Firebase Cloud Functions is on its way. Stay tuned..!!!

Thanks for visiting.

A single golf clap? Or a long standing ovation?

By clapping more or less, you can signal to us which stories really stand out.