paint-brush
Firebase server side fan-outby@Abhishek_Nandi
2,912 reads
2,912 reads

Firebase server side fan-out

by Abhishek NandiApril 23rd, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

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.

People Mentioned

Mention Thumbnail

Company Mentioned

Mention Thumbnail
featured image - Firebase server side fan-out
Abhishek Nandi HackerNoon profile picture

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.

https://www.odiocast.com

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


User Feeds, Firebase Server-side fanout and Google Cloud Functions by tech_in_startups_A discussion with Odiocast's Abhishek Nandi on how to use server side fanout for User Feeds on Firebase_www.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.

Representation of the firebase structure

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.