0

I have one database name user also having a last_seen field. I want to update this field in every 5sec with current time stamp.

sanu
  • 548
  • 7
  • 15

3 Answers3

1

Don't, just don't. You can easily do this with a periodical XHR-callback with any Javascript framework, but this means that if 50 users have your site open, you're going to be executing 10 requests per second, and your hoster is going to shut you down for generating ridiculous server load. If they don't, you're going to make your site ridiculously slow because of SQL database locks, or potentially even cause your entire site to time out on every request.

Just log the last pageview, and update the timestamps in other callbacks if there are any. If there aren't any other callbacks on the page the user is viewing there's no reasonable purpose anyway to know when he still had it open since the info he was viewing is majestically outdated anyway.

Niels Keurentjes
  • 41,402
  • 9
  • 98
  • 136
  • Like I said, you can, it's explained in http://stackoverflow.com/questions/5052543/how-to-fire-ajax-request-periodically. I'm just saying you **never, ever** should even **consider** doing this on a live site unless you have entire datacenters at your disposal like Facebook and Google. – Niels Keurentjes Aug 28 '13 at 11:40
0

You can make an AJAX call every 5 sec that's write that this user is active. However it will add a lot of traffic to your DB and webserver.

If it's some internal application and you know the load it could be fine. Also you can try to att the last_seen to memory in some kind of buffer whichs is written to disk if there is no more request in say 60 seconds

Simon Edström
  • 6,461
  • 7
  • 32
  • 52
0

Try mysql events:

http://dev.mysql.com/doc/refman/5.1/en/create-event.html

CREATE EVENT myevent
    ON SCHEDULE EVERY 5 SECOND
    DO
      UPDATE myschema.mytable SET mycol = mycol + 1;
trrrrrrm
  • 11,362
  • 25
  • 85
  • 130
  • 3
    And exactly how is MySQL supposed to know the user is still watching the page in question? – Niels Keurentjes Aug 28 '13 at 11:29
  • "How can I update my database with current time stamp in every 5 sec" This is his question, i guess it depends on his logic how to know if the user is still watching but what he asked for is a way to update the database each 5 seconds. – trrrrrrm Aug 28 '13 at 11:32
  • Technically this does answer the question, although I think the _implication_ was that the updates would only happen if the user is still on the page `:)`. – halfer Aug 28 '13 at 11:56
  • Why? It depends on the application, maybe he can add a column as a flig to indicate the user is online and keep updating the table each 5 seconds for all the online users, again it depends on the app and i just tried to help. :) – trrrrrrm Aug 28 '13 at 18:45