Nathan Wiles
Nathan Wiles

Reputation: 926

Timestamp From Client Side

I'm working on an instant messaging application, which records each message in a database with columns for message id, message content, and a timestamp for sorting.

I've been adding the timestamp using PHP's time() function right before inserting into the database, but it's occurred to me that this might cause issues. If a user submits two or more messages quickly, it's possible that the server could process them out of order and jumble them in the database.

I'm considering using javascript's Date.getTime() method instead. This way the timestamp records the time the user submitted the message, not the time it was finished being processed (hopefully those two numbers aren't that different).

This brings up a couple issues:

I'm sure this is an issue that's dealt with all the time. What kind of solutions are there?

Upvotes: 1

Views: 2011

Answers (1)

jdhildeb
jdhildeb

Reputation: 3811

Computers times are not well synchronized - your application definitely should NOT count on this.

Reasonable solution in my mind:

  • use the client timestamps ONLY for figuring out the sequence in which a client submitted messages.
  • use the server timestamp for everything else

Also note that client timestamps could be in any timezone (and users can change their timezone), so the client times really are only useful in comparing against other timestamps from the same client.

Upvotes: 7

Related Questions