I want to know if some one knows or have idea about making a JS Timer synchronised to all viewing users / clients ? I'm using PHP & Firebase as backends and vanilla JS & also Firebase for client side processing. The problem now is that, if a user / client leaves / closed the tab it's not getting the last time that it left that page.
Asked
Active
Viewed 41 times
0
-
AJAX + a database... – ArtisticPhoenix Jun 29 '16 at 02:24
-
[websockets](http://socketo.me/)? – Jorg Jun 29 '16 at 02:28
-
Sorry I forgot to add, that I'm using also Firebase for emitting date to viewing users. Please see the editted question above :) TIA – Kim Maravilla Jun 29 '16 at 02:30
-
While the idea is simple, execution is pretty difficult. With network lag and latency between servers, users and requests, it will be near impossible to get 100% synchronicity. You will need to include timestamps for each request start period, duration, offset, etc. How accurate do you need it to be? What is your time resolution? --- This was before the Firebase edit. – SArnab Jun 29 '16 at 02:30
-
If by leave the page, you mean closes the tab or goes to a different URL, you can use the `beforeunload` DOM event and fire a network request to store the current timestamp. There is no need for synchronicity if you base the time off when Firebase receives the request. – SArnab Jun 29 '16 at 02:32
-
@SArnab I listed that as an option, using `beforeunload`. But first I need to make sure that when a user visits that page, they will get the same time with the other users. – Kim Maravilla Jun 29 '16 at 02:39
-
Can I ask why you need them all to be on the same clock? And to what accuracy do you need that done? For a simple implementation, you could just broadcast a ping to all listeners on a channel with a running timestamp, but that's far from ideal. Another option would be to inject the server timestamp on page load, then count the seconds from there. Thus, all clients are running in reference to the server time. – SArnab Jun 29 '16 at 02:43
-
@SArnab because this application is like a workshop, there are facilitators which are viewing the workshop with a real time clock ( ticking every seconds ), so if one facilitator played the workshop it must start the clock synchronised to all facilitators, if another facilitator leave that workshop e.g by accidentally closing the browser or something happens to his PC, then if that facilitator went back to that workshop it should remember that last time that the browser closed or the last time since he left that workshop. – Kim Maravilla Jun 29 '16 at 03:19