Skip to content Skip to sidebar Skip to footer

How Do I Display The Followers Count In Real Time Using Firebase?

I have an application where users can follow other users. I want to have a real-time update system, display the total count of followers a user has. I've just started playing aroun

Solution 1:

Most of Firebase logic is based on listeners, you can add a listener to events happening in your collections and when those events happen, you do something.

One way to go about this would be:

var myFollowerListRef = new Firebase(PATH_TO_YOUR_COLLECTION);
myFollowerListRef.on("value", function(snapshot) {
    console.log(snapshot.length);
});

This way, every time your follower collection changes, the asynchronous function fires and you can do what you want with the fresh data.

For more information:

https://www.firebase.com/docs/web/guide/retrieving-data.html

Hope this helps, I'm still a beginner in Firebase.


Solution 2:

@Th0rndike's approach is the simplest and works fine for relatively short lists. For longer lists, consider using a transaction. From the Firebase documentation on saving transactional data:

var upvotesRef = new Firebase('https://docs-examples.firebaseio.com/android/saving-data/fireblog/posts/-JRHTHaIs-jNPLXOQivY/upvotes');
upvotesRef.transaction(function (current_value) {
  return (current_value || 0) + 1;
});

But I recommend that you read the entire Firebase guide. It contains solutions for a lot of common use-cases.


Post a Comment for "How Do I Display The Followers Count In Real Time Using Firebase?"