views:

28

answers:

1

I have an application where I would like to display the number of users that are logged into the system. Im trying to determine the cleanest way to do this that will not create a large load on the system.

I know that the system keeps track of the "last logged in time" So I could do a query and aggreate the number of users whose last login is within a given time?

Are there better solutions??

Any thoughts are welcome!! Please.

+1  A: 

Using the last logged in time doesn't work. Your sessions can last for days/weeks so you won't know the amount of active users that way.

One solution would be to create a table with login sessions where you store that session id in a session cookie. Session cookies expire once the user has closed his/her browser window so it should give you quite an accurate estimation of when people logged in. If you actually want to store the entire session duration, than you will also have to update the table with every page view to store the time that the user was last active. This would be slightly heavier for your database ofcourse.

WoLpH
is there a hook to do this in django registration? I can easily create a model for this but how would I access login view from django reegistration so that I can update this table???
DZ
@DZ: You can create a middleware to automatically update the table with every pageview. Just put the middleware _after_ the session middleware so you can see if someone is logged in.
WoLpH
Would that be rather DB intensive?
DZ
If you update a `last online at` column with every pageview. Than yes, it might be a little database intensive depending on how active your users are. If you simply check for a certain session cookie and only write a new one once a user enters the site. Than you will only have to do 1 database query per website visit.
WoLpH