How to add real-time notifications in Django applications

abdadeel
4 min readJul 25, 2022

--

Django realtime notifications with pubnub | abdadeel

Django to this day is a battle-tested web framework built with one of the most loved programming languages — Python. Its feature-rich has built-in support for crucial and critical things like user authentication, security, a powerful ORM (Object Relational Mapper), a built-in admin panel, forms, and much more already built into the framework.

But if we go back to the initial days of Django, there was barely a concept of microservices and event-based architecture. Monolith stacks were popular with minor cheeky touches to keep the stack meet the complex requirements of the time.

But today, web apps are much more modularized. We have excellent frontend frameworks like React, Vue, Svelte, etc providing far better UI/UX than templates. The world has shrunk and everyone now wants real-time experience when it comes to web apps. No more refreshes. No nothing. I want real-time changes!

But Django was never built to handle the real-time side of things e.g websockets, polling, etc. No doubt we have solutions like Django channels along with technologies like celery to solve the problem. But that too comes with more problems like:

  • Complexity — Django’s slogan The web framework for perfectionists with deadlines becomes stale.
  • Scalability — Scaling a real-time system is a challenge in itself. Combining it with having to build with tech that was not initially meant for it becomes super complex.

🎉 Let’s Solve The Problem

In this article, I’ll show you how you can add any sort of real-time functionality to your Django application while keeping the complexity and time to implementation minimal.

Here I’ll use PubNub to add notification features to a basic social media application I built as a demo. If you want to have a look at the codebase, here you go! 👇

Live App 👇

PubNub has been in the business for a really long time and their infrastructure is mature enough to handle a user base of any size you have and they have a very generous Free Tier 😉 as well. On top of that, you can either use their REST API for everything or use their SDKs. SDKs are available in pretty much all the server-side languages.

Application Description

This is a basic social media application. User can signup using their email and passwords. Create/Edit their posts. Users can comment on posts. Whenever a new comment is created, a notification is sent to the post owner.

Tech Stack

  • Django (Web Server)
  • Django REST framework (APIs)
  • PostgreSQL
  • PubNub (for real-time notifications)
  • Nextjs (frontend)

Click to see the full diagram

Server Logic

On the server, I have created basic Post and Comment models in users/models.py file. CRUD Rest APIs are handled using django restframework . Pretty basic route handlers using generic views and model serializers.

In server/core/pubnub/pubnub_service.py , I have created a basic utility class that exposes two basic functions from PubNub's python SDK. First, when the user logs in, a secret token has to be created for each user so that they can safely connect to PubNub. These authorization tokens make sure that only clients authorize to access a channel should allow reading messages from that channel.

Here I am creating a PubNub channel for each user. the name of that channel is the UUID of the user identified by custom get_notification_channel_name . When the user logs into my Django application, only that user will be granted a token to access the notifications channel associated with their unique id. This way, each user gets notifications intended for them.

Channels are fundamentals of how PubNub works. Your application can create as many PubNub channels as it needs. In fact, whenever you specify a channel in your request, if it does not already exist, it will be created before placing the message in the channel.

Django signals are being used to detect comments save events and on successful comment save, a notification is sent to the user through the signal receiver function. It’s that simple! Here is the simple receiver function.

Conclusion

This approach is not just limited to sending notifications, you can extend it to complex features like real-time chats, likes, etc. You get the point. This offloads handling real-time connections to PubNub. In my opinion, the cost is pretty comparable as well. If you handle all of this yourself, your team has to code all the real-time connections stack which takes both time and resources. On top of that, now your team has to take care of testing, maintaining, and scaling another infrastructure which is going to take even more resources as well. But this post should give you a good idea of which route you want to go.

If you like what you just read, don’t forget to follow me on Twitter 🐤, LinkedIn and Youtube. I share articles along with working examples and embeds so you can have a better reading/learning experience.

Here is the app link if you want to play with it.

See Ya!

--

--

abdadeel
abdadeel

Written by abdadeel

An engineer who loves to play around with tech and share learnings!