[Answered ]-How to get changes realtime with jquery in Django

2👍

If you are not going to use websockets and are going to use asynchronous calls then you can check for updates by running the AJAX call every so often:

var ajax_call = function() {
  var $people = $('#people');

  $.ajax({
      type: 'GET',
      url: '/all/api',
      success: function(people) {
        $.each(people, function(i, person) {
          $people.append('<li>name: ' + person.first_name+', last: '+ person.last_name + '</li>');
        });
      }
    });
};

var interval = 5000; // 5 secs
setInterval(ajax_call, interval);

You may want to include a ‘has_changed’ flag or return a 304 (Not Modified) status in the httpresponse if nothing has changed so that you can handle non-updates gracefully. You need to be careful with calls like this as people can leave pages open for days.

Leave a comment