2

I would like to implement a kind of "real-time" notification system in my Django application.

I would store some messages destined to a specific user in my database. When a user is logged in the application, if there is a notification for him in the database, then the application displays it using the messages framework. When he clicks on the message, it is deleted from the database.

I am a bit stuck on the "fetch the data every minute" thing. I heard of celery (http://docs.celeryproject.org/en/latest/#), but I would like to be sure it is the way to go there before diving in, because it seems a bit complicated to set up and use.

If there is an easy way to daemonise a django fonction, or if something similar to what I want to do already exist, I would appreciate any hint !

Johanna
  • 1,343
  • 4
  • 25
  • 44
  • In a word: Ajax. Define some view method for that specific task and call it within the javascript client code with Ajax. That's a simple way to get you started. – Paulo Bu May 13 '13 at 13:32
  • And how do you call this method periodically ? I'm not exactly an Ajax expert – Johanna May 13 '13 at 13:53
  • And other question, wouldn't this solution slow down a lot my application ? – Johanna May 13 '13 at 13:58
  • Actually, there are two ways of _daemonizing_ an internet app. *Ajax* (most common one) and recently *WebSockects* (which is not yet full supported by all browsers). Ajax works like _polling_. That means, querying your web server each minute. That's how Facebook works for instance. If is just a simple task then it would be very easy to set up. I'll write an answer to guide you. – Paulo Bu May 13 '13 at 14:04

2 Answers2

2

Here are some options to consider:

  • celery. Yes, it's not that trivial to set up, but it's very easy to configure and use from your python/django side via django-celery. Also take a look at celery periodic tasks.

  • rq (Redis Queue). Simple job queue that is easy to set up.

  • django-cronograph. Creating and running cron jobs easily from admin commands.

Also see:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
2

If is just a simple task, you may do with Ajax.

Just declare one URL for the ajax query:

#urls.py

...
url(r'^ajax/my_query$', my_app.views.ajax_processor)
...

then in your my_app/views.py:

#views.py

def ajax_processor(request):

    ... do the processing you want as if it is a normal web request.
    ... like querying the database
    ... you can return a `json` dictionary 
    ... or a normal `render_to_response` template with html

That should do on the server side. On the client side it would be lovely to use jQuery with the $.ajax function and do this:

$.ajax({
    url:'/ajax/my_query',  // a normal get request
    success:function(data){  // success is the callback when the server responds
        /* if is json what you decided to return then process the json dict
           if is normal html render it wherver you want
        */
    }
});

Is just a simple set up, a bit of code in server-side and a bit of code in client-side.

If you are planning to develop a heavily real-time based application then you should rely on a better library, but if you only need to do a little query asynchronous then you may consider this way.

Here is a good and simple ajax tutorial provided by the W3Schools to help you understand Ajax, and here you can find useful information about polling with ajax/jquery.

Good luck!

shahab
  • 313
  • 3
  • 16
Paulo Bu
  • 29,294
  • 6
  • 74
  • 73
  • Thank you for the details, I think I'll try that before trying celery. I guess my function should be simple enough to be handled by ajax. – Johanna May 13 '13 at 14:17
  • You're welcome. I added a link to an ajax tutorial to help you a little more. – Paulo Bu May 13 '13 at 14:22