0

Using firebase for a small messaging app. Using .on("child_added") functions which also work fine.

The issue is when the app is run, this function fires for all previous children and then stops.

Why does this behavior occur and how can we stop it?

Ikhlak S.
  • 8,578
  • 10
  • 57
  • 77
dylankbuckley
  • 1,507
  • 1
  • 13
  • 21
  • Firebase `child_added` events fire for all existing children on startup. That's simply how they are defined. To only get children that were added after the app started, have a look at https://stackoverflow.com/q/43440908, https://stackoverflow.com/q/34745248 – Frank van Puffelen May 26 '17 at 14:36

2 Answers2

0

Needed to use child_updated instead of child_added

Ikhlak S.
  • 8,578
  • 10
  • 57
  • 77
dylankbuckley
  • 1,507
  • 1
  • 13
  • 21
0

Use a flag.

The first time set a field in your object example:

let read_ts = (new Date()).getTime();

my_ref.on("child_added", snap => {
    if ( !snap.val().read_ts ){
        console.log("is new")
    } else {
        console.log("is old")
    }
});
Ikhlak S.
  • 8,578
  • 10
  • 57
  • 77