1

This is a question that I'm curious about, for perfectionism sake. It's not a problem that I'm completely stuck on.

I have a webpage that enables me to set a countdown timer. This countdown timer runs on the server, so an action can be executed when the timer finishes, even if the webpage has been closed.

I suspected it would be as simple as sending the endtime for the timer to the server, getting the current time on the server, set a timeout (NodeJS), and that's it. However, when I do this, the timer on the server finishes about 1.5 seconds before the one on the webpage, consistently. Like I said, this is not a big problem at all, but it made me wonder.

Something else I thought of, while thinking about this problem, was calculating the difference in time between now and the endtime on the webpage, sending this delta to the server, and setting a timeout with this delta. For my case (LAN only), I think this would pretty much "fix" the issue, since I don't expect any network hickups on my LAN. However, when something does go wrong, the timers obviously would be completely out of wack.

So how can I get these two timers "perfectly" synced?

Timmiej93
  • 1,328
  • 1
  • 16
  • 35
  • 1
    This is always going to be a problem, there is no way to be sure how off the two clocks are. – Adam H Jul 30 '18 at 21:18
  • 1
    If client and server are on the same LAN or same approximate network topology, then one simple thing is to have both client and server check with the same [NTP time server](https://www.ntppool.org/en/) and each make a calculation for how far off their system time is from the NTP time server and then compare notes to calc an offset. Repeat the process 5 times to average out any jitter. Note: this is more accurate if both client and server are in approximately the same network location since they should have about the same latency to the same NTP server. – jfriend00 Jul 30 '18 at 21:24
  • For my personal situation, I created a php file on my server which just returns the current time in milliseconds. Calling this from the client to get the start time, and calling it from the server to get the current time gives a very accurate time difference. I haven't been able to notice any lag myself. – Timmiej93 Aug 02 '18 at 10:42

0 Answers0