Reputation: 119
I wish to show the online status of the users of my website. I don't need to show the exact minut of the last activity. But certainly show if the user has had any activity in the past say 5-10 mins.
The problems i'm occouring when im thinking about creating this system are the following:
If i were to create a sql table with onlineUsers, and updated the table on every certain page, or every x page (to prevent too many sql updates) That would certainly give me a pretty precise number on minuts since the last activity. But it would cause too many connections with the database, which in the end would slow the website down.
I could somehow minimize this problem by making some smart equation, to only make the user update their onlineUsers row, every say 100 page click, which wouldnt require much resources from the php, but it would still be a grumpy way to do it..
I could also somehow integrate some kind of croplist system, that checked every 10th minut, which users that were online, and updated all the tables. But i'm not sure how that would work out, how it would check which users exactly had been active in the past 10 mins, without actually updating any table rows with their id's.
Oh well. I'm sure such a system has been made hundreds of times before.. So Someone should be able to tell me the best way to make such a system, that would require as little resources from my database as possible.
Thanks for the help :)
Upvotes: 0
Views: 687
Reputation: 76240
The method that is used, usually, is to just add a column to the user table called last_activity
which gets updated at the page load. It's the simplest and most reliable method you could possibly manage to create.
To speed up things you could create a session var that holds the last time you have updated the activity column. You could read that per user and check if the last update was too far away (let's say 3 minutes) and just update at that moment. This will avoid 100 database calls / minute if a user refresh the page 100 times in a minute.
I could somehow minimize this problem by making some smart equation, to only make the user update their onlineUsers row, every say 100 page click.
By page click I assume you meant page requests. And no that would be a bad idea IMHO: If a user loads the page 3 times in a period of time every 8 minutes it deserves to be updated each request.
I could also somehow integrate some kind of croplist system, that checked every 10th minut, which users that were online, and updated all the tables. But i'm not sure how that would work out, how it would check which users exactly had been active in the past 10 mins, without actually updating any table rows with their id's.
That is just an overkill.
Upvotes: 1