boom
boom

Reputation: 11686

Need client-side time to mirror server-side time

I need client-side time to mirror server-side time. Are there any best practices around this?

I'm not sure how exactly I would go about making sure the two match.

Thanks!

Upvotes: 2

Views: 156

Answers (2)

Matthew Bakaitis
Matthew Bakaitis

Reputation: 11990

The answer depends upon what you mean, as your phrasing doesn't really give enough info to know why you want the time to be the same across the different machines..

If you mean "I want all of the system clocks to be set to the very same time" then you need to search for stuff related to Network Time Protocol. Google can give you links to intro materials and there is also an overview of NTP on this site. But that's not a Node.js or programming problem...and the answers can be very operating system specific...and if you don't own all of the clients then you are out of luck

If you mean "I want all events to happen at the same time on all clients even if the system clocks are different" then there is already a question here that has all of these answers.

Good luck.

Upvotes: 2

Daniel
Daniel

Reputation: 38801

Network Time Protocol has an algorithm for synchronizing time between machines while accounting for network latency.

If you were to implement such an algorithm with AJAX or (even better) websockets, then you would end up with a value holding the differential between local machine time and server time.

Any time you did a time calculation on the client side, you could utilize that time differential value to adjust the output to be reasonably close to server time. It's never going to be exact, but you could probably get within 100 milliseconds on most connections.

Upvotes: 2

Related Questions