Skip to content Skip to sidebar Skip to footer

Synchronizing Javascript Clock With Server Clock

BACKGROUND I'm creating an AJAX chat system. It looks something like this: Mike - hi Jane - 5 minutes ago Jane - hi Mike - 4 minutes ago Mike - how's it going - 3 seconds ago Jane

Solution 1:

There's a limit to how precise you can be, as I explained in this answer -- in essence, any clock skew smaller than the request round-trip time is undetectable. If you really need the time to be as precise as possible, create a special request type to the server that just gets the time, and do whatever you can to make the round-trip as fast as possible.

You can identify browsers whose local clocks are clearly insane, though, because the observed server time will not fall between the request's local start and end time. Since you can't tighten it down much farther than that, just pick an arbitrary time within the local request [start, end] interval and treat that as being equivalent to the received server time.

Solution 2:

We've had a similar problem. Our solution was to simply ignore the browser clock. If the messages come in with a server timestamp, just sort them by that timestamp. Then all you need to do is when presenting the data, just convert that to browser time. All calculations and sorting were based off of server time though. This way, all you have to do is keep your servers times all synced.

Solution 3:

Use flash and a socket server. If you need to be 100% accurate. I have written a socket server for an application that broadcasts the time to a flash element every 1 second and tells it exactly what to display.

This is a financial based app where it needs to always be 100% accurate.

We also paired it up with FMS to be able to tunnel through corporate firewalls.

If you use javascript you won't be able to achieve accuracy.

possibly html5 will solve this issue with sockets soon

Post a Comment for "Synchronizing Javascript Clock With Server Clock"