2

I am using Angularfire2 and I'm tesing to build a chat app that pushes messages in the database. I want to get the time when the data is pushed. Your help is really appreciated. Thanks a lot!

2 Answers 2

2

Firebase uses a constant value which is replaced with a numeric timestamp when it is written to the database. This removes the need to track and synchronize the time across clients.

firebase.database.ServerValue.TIMESTAMP

You can then read the value of the reference that was just written to see the exact time.

var sessionsRef = firebase.database().ref('sessions');
var mySessionRef = sessionsRef.push();
mySessionRef.update({ startedAt: firebase.database.ServerValue.TIMESTAMP });
mySessionRef.once('value').then(function(dataSnapshot) {
  var time = dataSnapshot.child('startedAt'); // the time when the data was written
});
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, I saw this one, but how do you implement this in Angular 2 Typescript? Thanks!
This is just a value that you can include in any written data. I'm not sure I understand your question. Perhaps you can edit your question to describe exactly where you're stuck.
if you're using AngularFire2 you are using a wrapper around firebase and I don't see any access to ServerValue.TIMESTAMP either
0
firebase.database['ServerValue']['TIMESTAMP']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.