Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve firebase server time without setting it first

Tags:

php

firebase

Is there a way to obtain the server time without having to first set it? For example, I can simply do:

curl -X PUT -d '{".sv": "timestamp"}' https://SampleChat.firebaseIO-demo.com/servertime.json

which will return the server time, but this sets the value servertime within my firebase instance. I should note that I'm using the REST API.

like image 455
Prisoner Avatar asked Apr 17 '14 08:04

Prisoner


Video Answer


1 Answers

You can retrieve the server time without writing it by using .info/serverTimeOffset:

var fbRef = new Firebase(URL);

var getServerTime = (function(ref) {
    var offset = 0;
    ref.child('.info/serverTimeOffset').on('value', function(snap) {
       offset = snap.val();
    });

    return function() {
       return Date.now() + offset;
    }
})(fbRef);

var now = getServerTime();
like image 137
Kato Avatar answered Oct 21 '22 01:10

Kato