Firebase Cloud Functions : Introduction to triggers all you ever wanted

Chintan Soni
Apr 6, 2017 · 4 min read

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:

Image for post
Image for post

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.

Image for post
Image for post

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.”
Image for post
Image for post
  • So, there lies Event Providers (also known as Triggers) who provides:
  1. Event: to write Cloud Functions to listen for
  • For now, We are got Event Providers (Triggers) for:
  1. Realtime Database 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.

And what not…!!!

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

Image for post
Image for post

“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 ??

Image for post
Image for post

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

Thanks for visiting.

Zero Equals False

Zero Equals False delivers quality content to the Software community.

Sign up for Editors choice

By Zero Equals False

Zero Equals False - delivering quality content to the Software community. Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Chintan Soni

Written by

TechLead — Android at ARInspect | 7+ years of concrete experience in Android Development

Zero Equals False

Zero Equals False delivers quality content to the Software community. For more quality content, head over to https://zeroequalsfalse.com

Chintan Soni

Written by

TechLead — Android at ARInspect | 7+ years of concrete experience in Android Development

Zero Equals False

Zero Equals False delivers quality content to the Software community. For more quality content, head over to https://zeroequalsfalse.com

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store