2
votes

I need to create the notifications for future events. for example similar to reminders. I was thinking that I can use the the server timestamp + future time as the key and than pop only the notifications with the timestamp older than current server time. However I need the server time. I can't use client side time stamp as it might be out of sync.

Is there a way to get the current time stamp from firebase?

I know that there is a placeholder for timestamp which is replaced on the server side. Can I use this as a key? I can imagine that probably not so is there a way to listen to this event when the placeholder is replaced with real timestamp?

or generally is there any other sensible method around this problem?

1

1 Answers

1
votes

You're probably solving the X/Y problem here, and you might be better off explaining your use case and getting a better overall solution; fetching a server timestamp to use on the client seems extremely likely to be a conceptual problem.

For example, if my goal is just to display the time in messages, I can simple call set, monitor the path, and display what it returns:

var ref = new Firebase(URL);
ref.on('child_added', function(snap) {
   // client just listens on path, records have serve timestamp when they arrive
   console.log('the last event was added at', snap.val().time);
});
ref.push({ name: 'Kato', time: Firebase.ServerValue.TIMESTAMP });

If you are queuing future events, just store them in a different path and move them to the "present" path when ready. Rather than trying to fetch "now" and insert it later.

If you are set on fetching the timestamp, the simplest way would be to set up a dummy path and set the value against it:

new Firebase(URL).transaction(function(currValue) {
   return Firebase.ServerValue.TIMESTAMP;
}, function(err, success, snap) {
   console.log('the current server timestamp', snap.val());
});