Here is the simplest example possible:
var fb = new Firebase(\'https://xxxxxxxxxxx.firebaseio.com/test\');
fb.limitToLast(1).on(\'child_added\', function(sn
Firebase fires two local events for that write operation:
child_added
event with the local timestamp (corrected for your expected offset to the server)child_changed
event with the actual timestamp as the server specified it.So you can solve the problem by listening for both events:
var fb = new Firebase('https://xxxxxxxxxxx.firebaseio.com/test');
var query = fb.limitToLast(1);
query.on('child_added', function(snap) {
console.log('key', snap.key());
console.log('val', snap.val());
});
query.on('child_changed', function(snap) {
console.log('key', snap.key());
console.log('val', snap.val());
});
fb.push({
date_now: Firebase.ServerValue.TIMESTAMP
});
In general it is recommended to handle all child_*
events and not just child_added
. There may be more reasons why the server has to update or remove the value, to correct for the local event.
If you prefer having a single callback/event handler, you can also listen for the value
event:
var query = fb.limitToLast(1);
query.on('value', function(snap) {
snap.forEach(function(child) {
console.log('key', child.key());
console.log('val', child.val());
});
});
You'll note the use of forEach()
in the callback.
For the Realtime SDK this is the expected behavior. Eventually, the timestamp will reflect the proper server value. But, if this does not suit your needs you can use the REST API.
function addTimestamp() {
fetch('https://<my-firebase-app>.firebaseio.com/test/.json', {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
'.sv': 'timestamp' // add a timestamp
})
});
}
This will not use the local timestamp, and in combination with the Realtime SDK, each client will have the same timestamp on the first update.