Firebase realtime database triggers too many update events

Viewed 248

I'm using NodeJS, and listening for updates on an object using the following code:

let ref = mainRef.child("objectKey");
ref.on("value", (snapshot) => {
    console.log("update");
    console.log(snapshot.val());
});

Then I notice that sometimes, a single change causes 14 or 15 triggers of this events, bringing the latest state of the object, and some previous states as well.

The code above is running only once so there are no multiple registrations to the "value" event.

What's the problem?

Thanks

2 Answers

In my case I always got exactly two events for one change. I had used

firebase.database.ServerValue.TIMESTAMP

After removing the server timestamp from my document the value event would only fire once per update.

I thought I had the same problem as I was getting 40+ messages back from Firebase when I was updating just 1 attribute on 1 item.

Turns out that it wasn't the ref.on("value", ...) listener what was wrong but instead it was the code that triggered the actual update to Firebase, a bit of Javascript that was triggering the same AJAX call for the update was firing many times instead of just once.

So that'd be the first thing to check, maybe add some logging to make sure that the code that is updating the value on Firebase only runs once.

Related