Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to separate initial data load from incremental children with Firebase?

I have an application where new children get added to Firebase every 5 seconds or so. I have thousands of children.

On application load, I'd like to process the initial thousands differently from the subsequent children that trickle in every 5 seconds.

You might suggest I use value, process everything, and then use children_added. But I believe if the processing takes too long I have the potential to miss a point.

Is there a way to do this in Firebase that guarantees I don't miss a point?

like image 376
Keith Carter Avatar asked Jan 16 '15 05:01

Keith Carter


People also ask

What is limitToLast method in Firebase?

The limitToLast() method is used to set a maximum number of children to be synced for a given callback. If we set a limit of 100, we will initially only receive up to 100 child_added events. If we have less than 100 messages stored in our database, a child_added event will fire for each message.

Is there primary key in Firebase?

This chat application allows users to store the basic profile and contact list. The user profile would be located on a path such as Users/$uid. User is a node in it and will have a sort of primary key associated with an ID. So, we can access each one uniquely.


1 Answers

Since child_added events for the initial, pre-loaded data will fire before the value event fires on the parent, you can use the value event as a sort of "initial data loaded" notification. Here is some code I slightly modified from another similar StackOverflow question.

var initialDataLoaded = false; var ref = new Firebase('https://<your-Firebase>.firebaseio.com');  ref.on('child_added', function(snapshot) {   if (initialDataLoaded) {     var msg = snapshot.val().msg;     // do something here   } else {     // we are ignoring this child since it is pre-existing data   } });  ref.once('value', function(snapshot) {   initialDataLoaded = true; }); 

Thankfully, Firebase will smartly cache this data, meaning that creating both a child_added and a value listener will only download the data one time. Adding new Firebase listeners for data which has already crossed the wire is extremely cheap and you should feel comfortable doing things like that regularly.

If you are worried about downloading all that initial data when you don't actually need it, I would follow @FrankvanPuffelen's suggestions in the comments to use a timestamped query. This works really well and is optimized using Firebase queries.

like image 153
jwngr Avatar answered Sep 28 '22 07:09

jwngr