Weekend Django real-time notifications with SwampDragon

Displaying notification in different form is common these days. for example notification message notificat in in facebook and new mail notification by gmail.

Many people are concerned about implementing the the real time notification system correctly in a hassle free manner. And the good thing about this is that you can easily turn this out in a simple way.

This blog post provides an effortless yet a very handy way to implement a simple notification system in Django app.
This has been tested with Django 1.6.8 and 1.7.1, and python 2.7.4 with the following browsers:

Chrome 39.0.2171.71 (64-bit)
Safari 8.0 (10600.1.25.1)
Firefox developer edition 36.0a2 (2014-12-07)

NOTE: redis-server running is required. If you dont have redis installed see for
installaton procedures.

Complete source code can be downloaded from here :

Here is a step by step instructin for setting up everything up.

First step- Installation of SwampDragon.

Creating a new project is another step.

This is for project files and directories setup. Now we add paths to for the static files and HTML templates.  Open notifications/ in a text editor and add the following lines

Don’t forget to add demo to INSTALLED_APPS.

This is for tellign Django where to find our static resources and templates.

That’s all you need for setup.


We store the notification as a model. This way we can load existing notification when the user loads the page the first time.

Open demo/ in a text editor and add the following code

We have yet to create the NotificationSerializer so we will do that in the next step.

We add the SelfPublishModel mixin, so we don’t have to worry about actually publishing the model, as that will happen as soon as it’s created (note: for this post I have omitted handling updates to notifications).

Message field is the only field in this model, but a title to the notification can easily be added.

Here we are creating global notification, so everyone on the site will receive them. You could tailor the notifications to be on a per-user basis by adding a foreign key to the User model, and using swampdragon-auth to subscribe each user to their own notification channels (based on their username or some other unique identifier).


Add a new file: demo/ and open it in a text editor and add the following code

We only want to publish the message field, so we specify this in the publish_fieldsproperty.


In the absence of routers SwampDragon won’t know how to deal with the incoming data.

Create a new file: demo/ and add the following code



Since we don’t have anything generating notifications, we’ll add the Notification model to django admin so we can create them from there.

Open demo/ in a text editor and replace the content with the following:


loading the existing notifications is something we want now, and be able to show these notifications even if the SwampDragon server is not responding.

Open demo/ in a text editor and add the following code

Standard Django CBV (class based view) is used to load the five last notifications. We set the model to Notification and specify the template to be home.html.

Open notifications/ in a text editor and change it to the following code


That’s all the python code we have to write for this. Now a bit of javascript code and a template is to be added.


Create two new directories:

Create a new html template: templates/home.html and add the following


If you want to run this on anything but the local dev server, see this post on adding a context processor for the DRAGON_URL.


The last piece of the puzzle is the JavaScript.

Create a new JavaScript file: static/notifications.js and add the following:


Create a database

If you are using Django 1.7+


Create a super user. This is done by either answering “yes” to the question when you run syncdb or migrate. You can do it manually by running python createsuperuser.

Open a new terminal and type:

and another terminal and type

and also start redis server to another terminal

inorder to perform the test open a browser to http://localhost:8000. The browser will ask if localhost can have permission to show notifications. If your answer is no, you will only see the notifications on the page.

Open another browser window to http://localhost:8000/admin/. Log in and add a new notification instance and you should be able to see the new notification.

Additional Notes

As per the above explanation, you can have user specific notifications if you use something likeswampdragon-auth.

Update the model to the following:

A small change to the router is to be made by addingget_subscription_contexts and setting the @login_required decorator on the subscribeverb.


Now notifications are user specific (and users who are not signed in can simply not subscribe).

The example code for this post is available at

1264 Total Views 1 Views Today
  • Deiner David

    hi man i have a problem i get this error

    ConnectionError [ Errno 10061 ] You can not establish a connection because the machine DISCLAIMS specifically target the connection
    how i fix this plss i need modify mi notification system, because i used multiple gets settimeout thx