1

I am essentially trying to create a web app where one person can start a timer, and everyone else's timers (on different computers/phones) will start at the exact same time. I am currently using node.js and websockets. When the "master" timer hits start, the server uses websockets to tell all the devices to start. Since all the users should be on the same LAN, I thought I would not have to compensate for latency, but the timers are starting a few hundred milliseconds off of each other, and for my purposes, it is very noticeable. There isn't much delay between PCs, but mobile phones tend to be the most off of each other.

What would be the best way to get everything to start at the same exact time, within a margin of error of let's say, 50ms? I do not mind if the timers take a few extra seconds to start if the delay between them is within 50ms.

niCC_S
  • 11
  • 1
  • What does this timer control? And why is an accuracy of 50ms important? – t.niese Sep 18 '22 at 20:53
  • @t.niese the timer controls when audio plays, so if its off by more than 50ms it starts to become noticable – niCC_S Sep 18 '22 at 21:00
  • Ok so they might be in the same room having the speakers on? – t.niese Sep 18 '22 at 21:02
  • correct, the devices are in the same room – niCC_S Sep 18 '22 at 21:05
  • The problem is that you don't know if the time on the clients is equal, nor if what the delay causes, and if it is always the same. But you could try the approach described here [The best way to synchronize client-side javascript clock with server date](https://stackoverflow.com/questions/1638337/the-best-way-to-synchronize-client-side-javascript-clock-with-server-date). So measure the roundtrip time server-client-server, divide that by two. And send the current time and the latency from server to client to the client, the client can use that to estimate the offset needed. – t.niese Sep 18 '22 at 21:15

1 Answers1

0

Send a timestamp to the clients when to start the timer. Then the accuracy is tied to the accuracy of the system time.

If you can't ensure that the system time is accurate, another way would be to meassure latency and add it as an offset.

VarChar42
  • 727
  • 2
  • 13
  • Wouldn't that assume the client's system clocks are syncronized? Is there some way to sync them? – niCC_S Sep 18 '22 at 20:40
  • 1
    It would assume that the client will be able to fool you and start before/after others. – astef Sep 18 '22 at 20:44