node.js - Building a chat app: How to get time -


i building chat app pubnub. problem app/frontend point of view, how should time (server time). if every message sent server, server time there. 3rd party service pubnub, how can manage this? since app sends messages pubnub rather server. dont want rely on local time users might have inaccurate clocks.

the simplest solution thought of is: when app starts up, server time. record difference between local time , server time (diff = date.now() - servertime). when sending messages, time date.now() - diff. correct far?

i guess solution assumes 0 transmission (or latency) time? there more correct or recommended way implement this?

your use case reason why pubnub.time() exists.

in fact, have code example describing drift calculation.

https://github.com/pubnub/javascript/blob/1fa0b48227625f92de9460338c222152c853abda/examples/time-drift-detla-detection/drift-delta-detection.html

// drift functions function now(){ return+new date } function clock_drift(cb) {     clock_drift.start = now();     pubnub.time(function(timetoken){         var latency     = (now() - clock_drift.start) / 2         ,   server_time = (timetoken / 10000) + latency         ,   local_time  = now()         ,   drift       = local_time - server_time;         cb(drift);     });     if (clock_drift.ival) return;     clock_drift.ival = setinterval( function(){clock_drift(cb)}, 1000 ); } // how use code // periodically latency in miliseconds clock_drift(function(latency){     var out = pubnub.$('latency');     out.innerhtml = "clock drift delta: " + latency + "ms";     // flash update     pubnub.css( out, { background : latency > 2000 ? '#f32' : '#5b5' } );     settimeout( function() {         pubnub.css( out, { background : '#444' } );     }, 300 ); }); 

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -