Firebase Cloud Functions to Fan Data and Maintain Concurrency



Firebase Cloud Functions is new, incredible and powerful. Its allows you to run server-less functions when events happen on your Firebase Database. Its based off NodeJS and you can leverage a lot of the same capabilities. Here is Github repo of examples of things you can do. I highly recommend checking it out. I had some experience prior with Auth0/WebTask rules so there was some similarities.  You can read more about “fanning” out data here. I’m working on a new app that allows freelancers and employers to connect. I’m working on the contracting phase of the project. At the end of the day I want to be able to have all contracts in one place and each user to be able to see the status of their own contracts. Users can negotiate terms between in each other such as hours and rate.

Here are a few requirements but not all:

  1.  A /contract/ node that stores all contracts by contract id/key.
  2.  Each member in the contract needs a copy of the contract in their own node.
    • /user/contracts/contract_id
  3. We need to keep all copies in the database concurrent

Storing the contracts to their own node is easy. Fanning out the data to the members in each contract is not as straight forward. Not to mention maintaining concurrency when you’re de-normalizing your data for speed. When a change happens on the contract node, the contract data needs to change for all members of the contract.

Before Cloud Functions you would need to create a lookup node like /ContractsPerUser/user_id/contract_id:true. This way you could join user_id’s and contracts and perform the necessary look ups. You could write an atomic function to write the data out to the members when the contract is created but then you have to give the creator write permissions to another users data. That doesn’t sound very secure!

The better way to go about this would to perform this on the server. Here is the cloud function that I wrote along with them help of the getting started documentation on Firebase. You will need to have installed the firebase-cli, firebase-tools and login to firebase in your console.

Here is the code for updating and deleting nodes concurrently.

This will create a “fanContract” function that listens to events written to or deleted from the “/contracts/{contract_id}/ ” node.

The first part of the “if” statement says if there is no _delta data or no _newData then the node was deleted. Anytime you make changes a node it would return these values. So, if those values are null then remove the contract from the respective user contract nodes.

If there is _delta data and _newData then something changed with that node. You can access that information through “”. This event contains the data that was written as well as the push key. Now you can turn around and use the Admin SDK to write this data anywhere else in the database that you want.

It gets even better than this! Not only does it listen to new writes but it also listens to updates. If I go change any field in that contract, it’ll automatically preform the function and keep all the data for each member updated as well.

Once you deploy your function by using “firebase deploy –only functions –project <project-name>” you’ll see this:

There’s not too many tutorials and blog posts out there on the topic yet so I thought I’d share! Hope it helps you out.