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?