Real Time Database Triggers with Firebase Functions

A recent Android application I worked on required storing the latitude and longitude of a user’s location. I was asked to convert that latitude and longitude into a user-readable address. I could have written the code in Java, which would convert latitude and longitude into a user-readable address and store that information in the database along with the user’s latitude and longitude, but I was reminded that an iOS equivalent app would have to be made in the near future. This became a good opportunity to write some shared back-end code between the two apps. We considered using AWS Lambda, but because the back-end for both of the mobile apps would be Firebase, we opted to use Google’s Cloud Functions for instead.

Getting started

Step one is to create a new Firebase project associated with your Gmail account. There are plenty of great tutorials out there that will walk you through creating a Firebase project, so I’ll skip over that part. The second step is to install both Node.js and npm. You can visit the Node.js website to install both. After both Node.js and npm are installed, you can navigate to the folder you want to use to initialize your project via the command line GUI of your choice and run the command:

firebase login

You should be prompted with something similar to the following message:

After selecting yes / no, you’ll be taken to your Gmail login page where you will have to allow Firebase CLI to have some access to your Gmail account. After allowing this, we can return to our terminal and see that we have been successfully logged in! Now we can initialize our Firebase Cloud Functions project by running the command:

firebase init functions

In the folder you ran this command, you should see something like this:

If you  have more than one Firebase project associated with your account, you’ll be prompted to select which project you want to
initialize. After selecting your project, you should be asked to install all the Firebase dependencies via npm. Now that all of the setup is
done, we can get to the actual coding.

Writing our code

There are several triggers you can use to execute your code (outlined in the docs page). We are mainly concerned with the Realtime Database Trigger, but the HTTP trigger can be used as an alternate. If you navigate to the folder in which you ran the “firebase init functions” command, you should see a folder labeled “functions.” Inside that folder, there should be three items: index.js, package.json, and a folder titled node_modules. For my project, I used a library called geocoder to reverse geocode the user’s latitude and longitude. To install geocoder, navigate to the node_modules folder and run the command:

npm install geocoder

Once geocoder finishes installing, make sure to add it to the package.json mentioned earlier.

Next, use your text editor of choice open the index.js mentioned earlier. Your default file should look like this

To create a new function, we will add our own exports.functionName. The default index.js function is for creating an HTTP trigger, but as
mentioned before, a database trigger will work better for our needs. So to create a database trigger, we will write our function to look like

This tells the system we want to listen to any change in the /userEvents/{userId}/{eventId}/location and when a change occurs,
execute the function. I’m wild card matching userId and eventId in this so it will catch any change done to any user’s event in our node. The full structure of the database looks like this:

So now any time a change is made to the location field (either the latitude or the longitude field) for an event, our function will trigger! We can now get the user location from our database with the following code:

When our function triggers, the event variable in our code refers to a Delta Snapshot of the database.

Now that we have our user’s latitude and longitude, let’s create a promise to reverse geocode the latitude and longitude:

Within the promise, we make a call to geocoder.geocode, passing in a latitude and a longitude and receiving an address in return.

Now that we have a promise, let’s execute it, and store our results in the database:

In this code snippet we get the database reference of the “Position” field, so we can store a value in that node. We return the call to set
the result of our promise to a child of the Position node, called “address.” It is important to note that every function must end with a
return of a promise. So by looking at the reference docs for set, we can see that a void promise is returned.


That’s it! Now we can deploy our function to our project by opening a terminal in the same location we called “firebase init functions” and
running the command:

firebase deploy --only functions

If everything went well, you’ll see the function we created under your “Functions” page within the Firebase console. From there you can monitor execution time of your function, see any logs you added with theconsole.log command, and check usage statistics of your functions.


Leave a Reply

Your email address will not be published. Required fields are marked *