Connecting Firebase Cloud Functions with a Firebase Real-Time Database

With Node.js Scripts

Background:

Firebase cloud functions is a simple and easy way for developers to create server-less frameworks which automatically runs backend code in response to various triggers.

The trigger we will specifically explore today will be from a Firebase real-time database.

Steps to complete before getting started:

  1. Create a Firebase Project
  2. Create a Real-Time Database

Setting Up The Real-Time Database

Before you begin, make sure to have your Firebase database in test mode. This mode will allow you to read the database from cloud functions. After testing make sure to turn this mode off as it can soon develop into a cybersecurity threat.

First, let’s add some data to our database:

As shown above, the database has a top node of “parties”, with a sub node of “users”. The users node contains one value as of now which is of our user’s Name.

Setting Up Firebase Cloud Functions

Before setting up Firebase Cloud Functions, make sure to upgrade your billing tier to the Blaze Plan. Firebase Functions will be free on the Blaze Plan for up to 2 million invocations per month, so unless you’re running with heavy databases, your billing should be kept to a minimum.

Begin by first installing the Firebase CLI onto your machine and initialize Cloud Functions. Once you select your project, firebase cloud functions will be installed onto your machine. Complete instructions for this process can be found here.

Firebase Cloud Functions (onWrite)

**Navigate to the index.js file in your functions folder

Your functions folder should be located at a pathname such as: Users/johnapple/functions

Let’s first begin by installing the firebase packages into our node.js script, and initialize the database with Firebase admin

From here, you want to write a Real Time database trigger. A simple example of this would be for our cloud function to log some text such as “Hello World” once a new value is added to our database

For this functionality, you will be using the .onWrite event handler as a database trigger.

onWrite(), which triggers when data is created, updated, or deleted in Realtime Database. — Via Firebase Docs

As shown above, you now have created a function called “newNodeCreated” which watches the users node in our database. If data is created, updated or deleted in this node, you will be able to execute certain code.

So now, let’s fill the onWrite function with an action!

Your new variable “myMessage” holds the value of our data that has been updated, created, or deleted. This is done through the change.after method which eventually grabs the value of that object with .val().

Run firebase deploy in your terminal and wait for the function to deploy to the cloud. Once deployed, feel free to add, update, or delete data in your users node. Finally, check back into your function logs to see your code in action!

If you would be interested in a future article about connecting firebase cloud functions with Firebase cloud-messaging (FCM push notifications), feel free to comment down below! Thank you!

Contact:

Sohil Bhatia

Sohil Bhatia is currently a sophomore who is attending Redmond High School and is passionate about learning and using technology.

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