How to distinguish old items and new ones in firebase?

This question already has an answer here:

  • How to retrieve only new data?

    2 answers

I have this code:

 var database = firebase.database();
 var messages = database.ref('messages');
 messages.limitToLast(100).on('child_added', function(snapshot) {
     var data = snapshot.val();
     if (data.random != random_value) {

and it get executed when new message is added as well on init. How can I distinguish between new message and old messages?

On child_added it will always return all messages. On the first run it will return all those that already are in your database and after that it will just trigger when something is added that was not there before.

So if you just want new messages, you simply have to mark them as read in your database and go on from there.