2

I'm looking for a way to keep track of users that are online/offline. So if I present all users in a list i could have an icon or some kind of flag to show this. Is this built in in Django's default Auth system?

My first thought was to simply have a field in my profiles called last_logout in the models and update it with the date/time each time user logged out.

With this info and the built in last_login I should be able to make some kind of function to determine if the user is loggedin/online right?

Or should I just have a boolean field called "online" that I can change when user logs in and out?

Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
user3199840
  • 525
  • 2
  • 13
  • 35
  • possible duplicate of [How to get the list of the authenticated users?](http://stackoverflow.com/questions/2723052/how-to-get-the-list-of-the-authenticated-users) – Maxime Lorant Sep 10 '14 at 12:01
  • 1
    A user can login and then close the browser. Is that user online? It all depends on how you want it to be close to real time. – Germano Sep 10 '14 at 12:08

2 Answers2

5

With only django it will be hard to do. For such task async frameworks are more suitable.

For example, tornado.

Users won't do logout explicitly every time they go offline. They just close their browser and that's it. You can't know it with only django auth app. It is not designed for such tasks.

Even if you will check for not expired session, it not gives you all online users, because session can be non-expired for 30 days.

So to get real online users, possible solutions are:

  1. Every user will send some data via javascript to your server, for example every 10 seconds. You can fetch that data on server and put user into cache and set cache key to be alive for 10 seconds. So when you need to know, who are online now, you'll check your cache. But it is not a good solution, because it will need a lot of server resources.
  2. Use async framework (tornado) at server side (you can setup separate process for exact requests). And use websockets (SockJS is a good library for that at client side). It is a more complicated solution, but it is better.
stalk
  • 11,934
  • 4
  • 36
  • 58
  • Thank you for your answer! Feels like this was more complicated than I thought. I'm still in development and have not started on learning sessions and cache yet. I guess i need to go "live" for the second solution? – user3199840 Sep 10 '14 at 12:33
  • @user3199840, i would say, that it depends. If you don't expect big traffic on you app, you can choose 1st solution, as it is faster to implement. If you are not familiar with async frameworks, you'll need some time to dive in it. – stalk Sep 10 '14 at 12:36
  • Ok. Maybe it's better to just go with the built in last_login or some kind of last_activity timestamp-field instead to save time and performance. I just want the users to have some kind of feeling for when other users were active. Thanks again – user3199840 Sep 10 '14 at 12:48
3

You have to consider what exactly means for the users to be "online". Since any user can close the browser window any time and without the server knowing about that action, you'd end up having lots of false "online" users.

You have two basic options:

  • Keep track of the user's last activity time. Every time the user loads a page you'd update the value of the timer. To get a list of all online users you'd need to select the ones with an activity before X minutes. This is what is done by some web forums.

  • Open a websocket, long polling connection or some heartbeat to the server. This is what Facebook chat does. You'd need more than just django, since to keep a connection open another kind of server-side resources are needed.

vz0
  • 32,345
  • 7
  • 44
  • 77
  • 1
    Or perhaps just some Ajax that pings the server regularly. – Daniel Roseman Sep 10 '14 at 12:09
  • 1
    Thanks for you answer! I'm wondering. Can't you solve the "false" online users by having somekind of automatic log-out? Or would that lead to other unwanted problems? – user3199840 Sep 10 '14 at 12:26
  • 3
    @user3199840, That would lead to the unwanted problem of possibly logging out your users when they are active and in the middle of something :) – Paulo Almeida Sep 10 '14 at 13:15