How to keep track of online users?

44 views
Skip to first unread message

Branko Zivanovic

unread,
Mar 2, 2017, 5:47:30 PM3/2/17
to Django users
I need to know online status for each user. How do I do that?

Shawn Milochik

unread,
Mar 2, 2017, 6:40:16 PM3/2/17
to django...@googlegroups.com
You actually can't, unless you're using JavaScript for something like websockets or other polling. You can only track their last activity using their session.

Specifically, if a user loads one of your pages, you know it. But then you won't know whether they're still reading it hours later or they closed their browser immediately unless you use JavaScript in some form. 

Melvyn Sopacua

unread,
Mar 2, 2017, 7:22:54 PM3/2/17
to django...@googlegroups.com

On Thursday 02 March 2017 14:47:30 Branko Zivanovic wrote:

> I need to know online status for each user. How do I do that?

 

from importlib import import_module

from django.conf import settings

from django.contrib.auth import get_user_model

 

engine = import_module(settings.SESSION_ENGINE)

# docs

store = engine.SessionStore()

store.clear_expired()

logged_in = []

User = get_user_model()

 

for session in store.model.objects.all():

decoded = store.decode(session.session_data)

if '_auth_user_id' in decoded:

try:

user = User.objects.get(id=decoded['_auth_user_id']

logged_in.append(user)

except user.DoesNotExist:

pass

 

Of course, since by default the session length is 2 weeks, this doesn't tell you much, so if you want true "online status" you'd need to use your own session store to store the additional "last_online" field and middleware to update it automatically. Code above contains ample hints for you to start digging.

 

--

Melvyn Sopacua

Michal Petrucha

unread,
Mar 3, 2017, 3:28:55 AM3/3/17
to django...@googlegroups.com
On Fri, Mar 03, 2017 at 01:19:18AM +0100, Melvyn Sopacua wrote:
> On Thursday 02 March 2017 14:47:30 Branko Zivanovic wrote:
> > I need to know online status for each user. How do I do that?
>
> from importlib import import_module
> from django.conf import settings
> from django.contrib.auth import get_user_model
>
> engine = import_module(settings.SESSION_ENGINE)
> # docs[1]
> store = engine.SessionStore()
> store.clear_expired()
> logged_in = []
> User = get_user_model()
>
> for session in store.model.objects.all():
> decoded = store.decode(session.session_data)
> if '_auth_user_id' in decoded:
> try:
> user =
> User.objects.get(id=decoded['_auth_user_id']
> logged_in.append(user)
> except user.DoesNotExist:
> pass
>
> Of course, since by default the session length is 2 weeks, this doesn't tell you much,
> so if you want true "online status" you'd need to use your own session store to store
> the additional "last_online" field and middleware to update it automatically. Code
> above contains ample hints for you to start digging.

Keep in mind that this will only work if you use a model-based session
storage; if you use a storage like the signed_cookies session backend,
this will not be possible.

Cheers,

Michal
signature.asc
Reply all
Reply to author
Forward
0 new messages