Recently I wanted a simple solution to track whether a user is online on a given Django site. The definition of "online" on a site is kind of ambiguous, so I'll define that a user is considered to be online if they have made any request to the site in the last five minutes.
I found that one approach is to use Django's caching framework to track when a user last accessed the site. For example, upon each request, I can have a middleware set the current time as a cache value associated with a given user. This allows us to store some basic information about logged-in user's online state without having to hit the database on each request and easily retrieve it by accessing the cache.
My approach below. Comments welcome.
In settings.py:
In activeuser_middleware.py:
In your UserProfile module or some other model associated with the user:
I found that one approach is to use Django's caching framework to track when a user last accessed the site. For example, upon each request, I can have a middleware set the current time as a cache value associated with a given user. This allows us to store some basic information about logged-in user's online state without having to hit the database on each request and easily retrieve it by accessing the cache.
My approach below. Comments welcome.
In settings.py:
# add the middleware that you are about to create to settings MIDDLEWARE_CLASSES = ( .... 'middleware.activeuser_middleware.ActiveUserMiddleware', .... ) # Setup caching per Django docs. In actuality, you'd probably use memcached instead of local memory. CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', 'LOCATION': 'default-cache' } } # Number of seconds of inactivity before a user is marked offline USER_ONLINE_TIMEOUT = 300 # Number of seconds that we will keep track of inactive users for before # their last seen is removed from the cache USER_LASTSEEN_TIMEOUT = 60 * 60 * 24 * 7
In activeuser_middleware.py:
import datetime from django.core.cache import cache from django.conf import settings class ActiveUserMiddleware: def process_request(self, request): current_user = request.user if request.user.is_authenticated(): now = datetime.datetime.now() cache.set('seen_%s' % (current_user.username), now, settings.USER_LASTSEEN_TIMEOUT)
In your UserProfile module or some other model associated with the user:
class UserProfile(models.Model): .... .... def last_seen(self): return cache.get('seen_%s' % self.user.username) def online(self): if self.last_seen(): now = datetime.datetime.now() if now > self.last_seen() + datetime.timedelta( seconds=settings.USER_ONLINE_TIMEOUT): return False else: return True else: return False
Then in the template where you want to display whether the user is online or not:
Pros:
- Simple solution
- Doesn't need to hit the database for saving the timestamp each request
Cons:
- Last user access times are cleared if the server is rebooted or cache is reset
- Last user access times are accessible only as long as they exist in the cache.
{% with request.user.get_profile as profile %} <table> <tr><th>Last Seen</th><td>{% if profile.last_seen %}{{ profile.last_seen|timesince }}{% else %}awhile{% endif %} ago</td></tr> <tr><th>Online</th><td>{{ profile.online }}</td></tr> </table> {% endwith %}
Pros:
- Simple solution
- Doesn't need to hit the database for saving the timestamp each request
Cons:
- Last user access times are cleared if the server is rebooted or cache is reset
- Last user access times are accessible only as long as they exist in the cache.
there is a typo in the title ;)
ReplyDeleteLove this simple idea ;)
ReplyDeleteThere is also a gist from Eric Florenzano https://gist.github.com/268379 which uses similar approach, but stores list of user ids under one key.
ReplyDeleteRedis is a really good fit for this type of data due to supporting data structures. A sorted set of user ids by last seen time does the trick for this scenario.
ReplyDeleteThis is how I did it in my forum app using Redis, including tracking of where in the app a user was last seen for display on their profile page:
https://github.com/insin/forum/blob/master/forum/redis_connection.py#L69-96
Typo fixed :-) Thanks for the other approaches. I noticed that a limitation of my approach above is that there is currently no easy way to obtain a full list of users.
ReplyDeleteI thought about about the same technique and put it in practice recently.
ReplyDeleteThe difference is that it seems to combine your technique and Eric Florenzano's one : I have a set of keys, one per user, with the last access, and another key, which is a list of online users.
Offline users are deleted from the list when needed.
Joe - Looking to speak to you. I came across your website here and would like to talk to you about this Python/Django role in Lake Success, NY I'm currently working on. When would be a good time to talk?
ReplyDeleteJason
i got an error, running the project....
ReplyDeleteImproperlyConfigured: Error importing middleware middleware.activeuser_middleware: "No module named middleware.activeuser_middleware"
Good tutorial!
ReplyDeleteHow about if I want to track who's viewed my user profile on django just like on LinkedIn?
write a decorator around view function which render your user profile or user detail and track who wants to see this page. and your request has the current user surely
Deletemw_instance = middleware(handler)
ReplyDeleteTypeError: ActiveUserMiddleware() takes no arguments
It's not working, showing nothing in the template.
ReplyDeleteThe blog you shared is very good. I expect more information from you like this blog. Thank you.
ReplyDeletewhat is overloading
important libraries in python
substring function in python
data science linear regression in python
j2ee interview questions
python operator overloading
I read your article it is very interesting and every concept is very clear, thank you so much for sharing. AWS Certification Course in Chennai
ReplyDeleteI am definitely enjoying your website. You definitely have some great insight and great stories.
ReplyDeletetv series jackets
How about if i want the tracking in realtime with django channels. Thanks.
ReplyDeleteI like your post. I appreciate your blogs because they are really good. Please go to this website for Data analyst course in Bangalore. These courses are wonderful for professionals.
ReplyDelete