Skip to main content

Django: Using Caching to Track Online Users

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:
# 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:
{% 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.

Comments

  1. there is a typo in the title ;)

    ReplyDelete
  2. There 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.

    ReplyDelete
  3. Redis 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.

    This 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

    ReplyDelete
  4. 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.

    ReplyDelete
  5. I thought about about the same technique and put it in practice recently.
    The 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.

    ReplyDelete
  6. 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?

    Jason

    ReplyDelete
  7. i got an error, running the project....
    ImproperlyConfigured: Error importing middleware middleware.activeuser_middleware: "No module named middleware.activeuser_middleware"

    ReplyDelete
  8. Good tutorial!
    How about if I want to track who's viewed my user profile on django just like on LinkedIn?

    ReplyDelete
    Replies
    1. 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

      Delete
  9. mw_instance = middleware(handler)
    TypeError: ActiveUserMiddleware() takes no arguments

    ReplyDelete
  10. It's not working, showing nothing in the template.

    ReplyDelete
  11. 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

    ReplyDelete
  12. How about if i want the tracking in realtime with django channels. Thanks.

    ReplyDelete
  13. Broken Windshield Solution

    Broken windshield can be a major inconvenience for drivers, especially if it happens unexpectedly. Whether it's a small crack or a shattered windshield, there are several solutions available to get you back on the road safely.
    The first step in addressing a broken windshield is to assess the damage. If the damage is minor, such as a small chip or crack, it may be possible to repair the windshield instead of replacing it. Windshield repair is a relatively quick and inexpensive process that involves injecting a special resin into the damaged area. The resin fills in the crack or chip and restores the structural integrity of the windshield. However, it's important to address small chips and cracks as soon as possible, as they can quickly spread and become more difficult to repair.
    If the damage is more significant, such as a larger crack or shattered glass, the windshield will likely need to be replaced. Windshield replacement involves removing the old windshield and installing a new one. This is a more time-consuming and expensive process than repair, but it's necessary for more severe damage.
    It's important to have a professional handle windshield repair or replacement. Improperly installed windshields can be dangerous in the event of an accident, so it's important to choose a reputable and experienced auto glass service provider.
    In the meantime, there are temporary solutions available to prevent the damage from getting worse. One option is to use a windshield repair kit, which conyou need to fill in the crack or chip yourself. These kits are relatively inexpensive and can be found at most auto parts stores. Another option is to cover the crack or chip with clear packing tape. This won't fix the problem, but it can prevent dirt and debris from getting into the damaged area and making it worse.
    To prevent a broken windshield in the first place, there are several steps you can take. Avoid driving behind large trucks or other vehicles that may kick up debris, especially on highways and construction zones. Use caution when driving on rough roads or over speed bumps, and make sure your windshield wipers are in good condition to remove dirt and debris from your windshield.



    ReplyDelete
  14. Very Good And Useful information Thank For Sharing if you want to join - Payment Gateway services in east delhi visit Vcare Technical Institute.

    ReplyDelete

Post a Comment

Popular posts from this blog

Docker: Run as non root user

It's good practice to run processes within a container as a non-root user with restricted permissions.  Even though containers are isolated from the host operating system, they do share the same kernel as the host. Also, processes within a container should be prevented from writing to where they shouldn't be allowed to as extra protection against exploitation. Running a Docker process as a non-root user has been a Docker feature as of version 1.10. To run a Docker process as a non-root user, permissions need to be accounted for meticulously.  This permission adjustment needs to be done when building a Dockerfile. You need to be aware of where in the filesystem your app might write to, and adjust the permissions accordingly.  Since everything in a container is considered disposable, the container process really shouldn't be writing to too many locations once build. Here is an annotated example of how you might create a Dockerfile where the process that runs within runs a

Automatic Maintenance Page for Nginx+Django app

If you've used Django with Nginx, you are probably familiar with how to configure the Nginx process group to reverse proxy to a second Gunicorn or uWSGI Django process group.  (The proxy_pass Nginx parameter passes traffic through Nginx to Django.) One benefit of this approach is that if your Django process crashes or if you are preforming an upgrade and take Django offline, Nginx can still be available to serve static content and offer some sort of "the system is down" message to users.  With just a few lines of configuration, Nginx can be set to automatically display a splash page in the above scenarios. If the Django process running behind the reverse proxy becomes unavailable, a 502 error will be emitted by Nginx.  By default, that 502 will be returned to the browser as an ugly error message.  However, Nginx can be configured to catch that 502 error and respond with custom behavior.  Specifically, if a 502 is raised by Nginx, Nginx can check for a custom html erro