2

I have created a chat with node.js and socket.io.

When a user sends a message, I insert it directly in the DOM and emit the message to the server, so it can be emitted to all the other clients.

The problem is that it seems the timestamp set with Date.now() on the server is different from the timestamp set on the client with exact same command.

This makes the interface a bit weird because a message sent at a later point in time can show a timestamp prior to previously sent messages.

One solution would be to calculate the time difference when the user joins the chat room and subtract this difference when a new message is added, but should this really be necessary or is this the common way to solve this problem? Could this also be the solution to cope with timezones etc?

mortensen
  • 1,167
  • 2
  • 13
  • 23
  • 1
    The clocks on different computers will differ - perhaps always use the timestamp when message received at server? The time difference approach will help but is complicated by the fact that the time to deliver message from client to server is not constant. – John D Oct 08 '16 at 06:40
  • But I want to append the user's message before sending it to the server. How does websites as Facebook do it in their chat? – mortensen Oct 08 '16 at 08:01
  • I think clients send messages to the server, receive the server timestamp, and then everyone sees them with time of arrival at server. – John D Oct 08 '16 at 13:46

1 Answers1

0

Different time zones between the client and the server possibly. It will not work with multiple users across the world. This one will be helpful:

How to ignore user's time zone and force Date() use specific time zone

Also, if you append the timestamp to the DOM immediately, and then get the date again from the server, there will be a difference in any case because you have to consider the time that the request needs to reach the server.

Community
  • 1
  • 1
Stavros Zavrakas
  • 3,045
  • 1
  • 17
  • 30
  • `Date.now()` returns a Unix timestamp, which is timezoneless. – robertklep Oct 08 '16 at 07:41
  • @robertklep Doesn't it depend on the browser's time zone of each user? Also, on node doesn't it depend on the configuration of the server? – Stavros Zavrakas Oct 08 '16 at 07:59
  • No, timestamps are always relative to UTC. See also [this question and its answers](http://stackoverflow.com/questions/23062515/do-unix-timestamps-change-across-timezones). – robertklep Oct 08 '16 at 08:22
  • @robertklep there is a comment in there (not the acceptable answer though): "IF both computers are set up correctly with their clocks set for the correct timezone and UTC values, they should return the same value. Of course that's a big IF." – Stavros Zavrakas Oct 08 '16 at 08:27
  • Yes, the main issue here is not having different timezones, but clocks that are out of sync: the browser's computer saying it's 9:23 (relative to UTC), the server saying it's 9:18 (relative to UTC), for instance. – robertklep Oct 08 '16 at 08:31
  • @robertklep—not so much timezoneless but all in the same time zone. ;-) – RobG Oct 08 '16 at 10:50
  • @RobG AFAIK, UTC isn't considered a time zone =D – robertklep Oct 08 '16 at 11:36
  • @robertklep—yes, it's a standard, I should have added "…with zero offset". :-) – RobG Oct 08 '16 at 12:05