Search code examples
javascriptjqueryajaxangularjscomet

Make ajax call based on change of server response?


I want to make an ajax request when and only when there is a change in server... currently I am using comet technique... I make a request to the server and waits for the response. When the response comes, after one second I again send request to the server.

I have used angularjs for this process... Here is my code...

myapp.run(function(Poller) {});
        myapp.factory('Poller', function($http, $timeout) {

          var data = { response: {}, calls: 0 };
          var poller = function() {

        $http.get('http://example.com').success(function(data, status, headers, config) {
              data.response = data;
              $('.serverStatus').text(data.response.message);
              data.calls++;
              $timeout(poller, 1000);
            });
          };
          poller();

          return {
            data: data
          };
        });

But the problem is that it is putting too much load on the server... If I would have thousands of users using my application my server would stuck.

My page should only call the server when there is a change in server (like db, or json etc). Please help me in this


Solution

  • Start a web socket at the server and you can connect using that