Real time audience polling with Firebase

Real time database provides very nice platform for experimentation and dwelling onto your creativity. As the speed of necessity in fetching/getting the information increases along with the extra constraint of data changing all the time, real time databases have proved to be very useful.

Now in a massive real time database, it would be great if you are informed of the changes happening in certain part of data, where you are interested in, in real time, as the change happens. In design pattern, this model will be referred to as publisher-subscriber model.

There exists several third party cloud service providers which will help you in achieving the publish-subscribe pattern, but let us focus on Firebase in this article as it has excellent real-time database service along with other essential services of notifications, file storage, analytics etc. Let us build one exciting real-time audience polling app with Firebase in which question will be asked to audience which has to be answered in few seconds time and admin gets to see the result in real time. Take a look at this video.

If you felt it was too fast to understand, what happened was the publisher app on left side published a question in Sports channel. The subscriber app on right side was tuned to Sports channel and hence it received the question. Upon tapping the bubble, the question was answered and the publisher app got the live update of poll results in real time.

Now let’s take a look at the simple architecture of our product to understand how is it that person who was tuned into a particular channel received the question.

As you can see, Firebase has taken the heavy burden of work of keeping track of subscribers of each channel separately and once the admin publishes a question, Firebase does the heavy lifting work of sending the question to all the subscribers of that channel! Make a note that we are not involving push notifications in this to send the question. The secret is whenever any changes are made inside the nodes (or channels in above picture), Firebase sends the corresponding changes to all the listeners (subscribers) of that node.

Inside the Firebase, data is stored in a series of dictionaries. Data can be easily scaled and more sub-nodes are created upon addition of channels, questions or answers. A snapshot of Firebase database is shown in this picture.

Firebase database structure

Next, let us look into the simple code of how the data is stored in Firebase.

Storing data in Firebase

In the subscriber app, we will have to express our interest of listening to changes in channel nodes of Firebase by the following code.

Listening to nodes in Firebase

As you are already beginning to get intuition, the same procedure of listening to question nodes by subscriber app, is being used in publisher app to listen to answer nodes. And the answers are uploaded by subscriber app into the Firebase.

As you can see, Firebase provides a very good real time database and with little amount of coding we have created a real time audience polling app. But there are certain downsides and limitations with Firebase which I would like to highlight in an another article.

Reading this article, if you are interested in getting started with Firebase, you may check out with this Setting up Firebase tutorial. You can check out the full code being used in this project in the Github repository.

Developed at Innovation Labs @ Y Media Labs