Pergunta

I have been using django for almost 6 months now and it works fine for almost all kind of websites I do.

However , recently I stumbled upon an issue when I was doing a website, where a user gets notified about any blog post the other user updates.

My approach was this :

From my template I keep doing an ajax call like this :

 $(document).ready(function() {
   setInterval(function(){get_updates();}, 10000);
   function get_updates(){
    $.ajax({
      type: "GET",
      datatype: 'json',
      url: "{% url 'models.views.update_notification' %}",
      success: function(data) { 
        if(data.updated){
       $.("content").load('notifications.html');
        }
      }
    })
  })
});

}
class UpdateNotificationView(View):
    def get(self, request):
        user = FriendUser.objects.get(name='friend')
        msg = {"updated" : user.is_updated()}
        return HttpResponse(simplejson.dumps(msg))

Assuming that notifications.html is just a partial included in every page:

 <div id='divid'>{{ notification }}</div>

The problem here is that , I don't think its a good Idea to keep doing ajax calls like this every x seconds/minutes.

Is there a way to push updates from the backend as soon as a database is updated directly to the browser , without polling for updates like this ?

Or is django not made up for it ?

Foi útil?

Solução

I guess you should have a look at django-comet or any othe comet-like project.
It will allow you to push a response from server to the user's browser.

Wiki page on Comet: http://en.wikipedia.org/wiki/Comet_(programming)

Also, let me share a link to a great answer on this topic: https://stackoverflow.com/a/4403209/2795926

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top