In this post I will describe how we at Odiocast efficiently used server side fanouts leveraging Firebase Functions and Realtime Database
In October of 2015, Firebase team published an article which described how to use client side fanout, where you write or send one object to the server and the server rather than writing several times to different database locations. One such use case is a feed for a social network.
Odiocast has a user feed, where you see the stories published by your network and also listen to it. In certain cases, data replication can be beneficial and feed is one of those cases. Instead of using client side fan out, we built a server side fanout where when a user posts an audio stories, it gets replicated to the feed of all the users in the audience network of that publisher/creator. For this we use Firebase Cloud Functions.
Listen to this on Odiocast.com
A discussion with Odiocast's Abhishek Nandi on how to use server side fanout for User Feeds on Firebasewww.odiocast.com
So lets go through the firebase database structure that we use. Lets say you have a node for stories which has data for all stories published by each user and another called followers which has all user follower information.
So when a user publishes a story, a new node is added to the stories node. To populate the feed for every follower of that user, this story needs to be replicated to all feed nodes of individual users. So we will need to write a Firebase Database trigger for onWrite event for the specific node.
The picture might look alot simpler than the actual structure, this has been intentionally done to explain the function rather than the database structure.
So whenever something is written to a single story of any user, the firebase functions is triggered where we iterate through the followers of the publisher and start adding the story to the feed nodes of these users.
Code Snippet for the firebase function
This is design is a multiple write executed on the server side using Cloud Functions.
There are certain things you need to consider, this function will be triggered whenever a write event happens on any of the child or root of an individual story. It will also be triggered for node deletion. There are multiple ways in which this can be handled and detected. the event object has methods using which you can check if a specific node was changed or not.
While the above snippet can be optimized further with a transaction, I would leave that to anyone who wants to implement something similar.
In my previous post I had mentioned how Cloud Functions makes Firebase a powerful platform, this post also portrays the same.
For more such articles, Follow me on Medium, and recommend ❤ this post.
Hacker Noon is how hackers start their afternoons. We’re a part of the @AMI family. We are now accepting submissions and happy to discuss advertising & sponsorship opportunities.
If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!