Will & Skill Developers

Will & Skill Developers

Thoughts, snippets and ideas from the team at Will & Skill AB, Stockholm.

Hassan Mian

Don't let someone else's opinion of you become your reality. - Les Brown




Real Time Notifications with Firebase, Django and Backbone - Part 1

Hassan MianHassan Mian

Most social projects need a notification system. We need to inform users when a comment has been made on a post they have authored or if someone has commented on a post they have commented on. We also use notifications to tell users when admins/managers upload new files.

Traditionally this is accomplished through a simple email. Lately we looked into the possibility to "push" notifications directly to the client. We did some initial research and found Firebase, a simple JSON-storage with realtime functionality.

I'll go through some simple steps and thoughts regarding desktop notifications.

First of all we need to setup some django-signals. These should be specific for your project, so I'll just take a simple case: "Whenever a NEW Blog Post is created, we need to notify ALL users." Simple enough!

Our Model:

from django.contrib.auth.models import User
from django.db import models
from django.db.models.signals import post_save

class Post(TimeStampedModel):

	title = models.CharField(max_length=512)
    content = models.TextField()
	author = models.ForeignKey(User)
    def get_subscribers(self):
    	users = User.objects.all()
    	users = users.exclude(pk=self.author.pk)
    	return users

Notice that I've imported User, models and post_save.

  • User because we need to connect our Post to a User.
  • models for obvious reasons :)
  • post_save because we want to trigger a function whenever a Blog Post has been saved.
  • I've also added a method called get_subscribers(). We will call this function whenever we need a list of users that should be notified when we create a notification for Blog Posts. We also exclude the author.

So now we have a simple model with one method.

That's all for part 1. Lets take a look at our post_save method in part 2

Take me to part 2

Hassan Mian

Hassan Mian

Don't let someone else's opinion of you become your reality. - Les Brown

View Comments