Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase.ServerValue.TIMESTAMP returns different value on immediate child_added

I have a Firebase Push with Server Timestamp, where a child_added listener is already listening.

Extremely surprisingly, the value returned to the child_added event is ~300ms different from what is in fact stored in the Firebase Database node. Subsequent reads at the same node, provides the 'correct' time as stored at the node. It is only the initial child_added event that is triggered right when the push is occurring, that is returning a wrong time.

Has anyone observed this issue?

ref = new Firebase(/some/path);
ref.on('child_added', function (childData) {
   console.log('childData.pushTime = ' + childData.pushTime);
});
ref.push({pushTime: Firebase.ServerValue.TIMESTAMP});

What the console.log prints is off by ~300ms from what is in fact stored at the path.

like image 801
Nat Avatar asked Dec 14 '25 02:12

Nat


1 Answers

The behaviour that you are seeing is the expected behaviour.

It occurs because the child_added event is fired locally, using a value for ServerValue.TIMESTAMP that's determined using the local time and the offset of the local clock from the server clock. However, it's not possible to account for the time it will take for the request to arrive at the server - hence the differing timestamps.

If you also listen to the child_changed event, it will fire with a snapshot that includes the correct time once the write is complete.

like image 74
cartant Avatar answered Dec 16 '25 10:12

cartant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!