To track the elements added from a certain reference point and without obtaining the previous records, you can use endAt()
and limit()
, for example, to obtain the last record:
// recuperar el ultimo registro de 'ref'
ref.endAt().limit(1).on('child_added', function(snapshot) {
//todos los registros despues del ultimo continuan para invocar esta funcion
console.log(snapshot.name(), snapshot.val());
});
Another option may be to use limitToLast()
and limitToFirst()
, which replace limit()
.
// recuperar el ultimo registro de 'ref'
ref.limitToLast(1).on('child_added', function(snapshot) {
// todos los registros despues del ultimo continuan para invocar esta funcion
console.log(snapshot.name(), snapshot.val());
// obtener la ultima llave insertada
console.log(snapshot.key());
});
And finally I attached this link where there are some example codes: link
I hope it serves you.