I've been working with django for about 6 months now and it has been effective for the websites I create.
However, I recently encountered an issue while developing a website where users receive notifications whenever another user updates a blog post.
My initial approach was to continuously make ajax calls from my template 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 simply a partial included in every page:
<div id='divid'>{{ notification }}</div>
The concern here is that constantly making ajax calls like this at regular intervals may not be the most efficient solution.
Is there a way to send updates directly from the backend to the browser as soon as the database is updated without relying on periodic polling for updates like this?
Or is django not equipped for this type of functionality?