Blog Post Icon
Blog
05/01/2015

Ajax Long Polling to your RESTful API

Hello!

Whether your developing a mobile or web based application, keeping on top of constantly changing data is a challenge on its own. Sometimes its necessary to make repeated calls to your API to pull updates at regular intervals.

We wrote a previous blog post describing how to use Ajax to pull JSON data via a RESTful web API (click here to read it).

Somewhat extending that sentiment a bit, we would like to walk through the process of creating an Ajax “polling” process that repeatedly polls the API to pull data over and over again. This will allow you to receive, process and manipulate or display the data as it changes.

The context through which we are using this process is to display changes made to a google map via geo-location updates to a centralized API server that processes updates and returns them to the end users. This particular context is within an Angular Javascript based framework, but it really can be used anywhere that supports Ajax + Javascript.

Here’s the first part of the polling process :

               (function poll() {
                setTimeout(function() {
                           $.ajax({
                                  url: "https://yoursite.com/request",
                                  headers: {
                                  'Content-Type': 'application/x-www-form-urlencoded'
                                  },
                                  type: "GET",
                                  success: function(data) {

What we’re doing above is defining a “poll” function that will essentially be calling itself at set intervals. Within the function there will be a “success” and “error” function nested within. Those functions can be utilized to execute additional checks and balances in order to ensure the data is sanitized and to catch any subsequent errors at every step of the way.

For example, in the “success” function, you could do something like this :

       success: function(data) {
           // check if null return (no results from API)
           if (data == null) {
               // do something if no data is returned, perhaps display static info, reset some arrays or display an error message
          } else { 
               $.each(data, function(index, element) {
                   arrayName.push(element.whatever);
          }
       }

What were doing is checking if the data pulled from the Ajax request is null. If its null, we can do various things such as clear variables or display an error message perhaps. If its not empty, then we will iterate what we will be expecting to be an array of data. We then “push” each iteration into our own array apty named arrayName.

The next step is key. Because we are pulling data at polling intervals, its possible that we will see data that already exists in our array (or ultimately wherever we would be internally storing the data). What we want to do is implement a check within the poll to see if the data returned actually matches what we might already have. If a match happens, then we probably want to ignore it. This is more of a requirement specific to our needs in the context of our project, but I thought to include it here because its interesting and hopefully useful to others.

for (var i = 0; i < arrayName.length; i++) {
    // check if new data received matches what is in internal array
    if (arrayName[i][0] === element.whatever1 && arrayName[i][1] === element.whatever2) {
        console.log('data received is already in array .. skipping');
    } else {
        console.log('new data received! processing ...');
    }

What I'm doing above (in the context of google maps) is checking the latitude and longitude of our internal array against the iterated data. As such, we need to check both the latitude and longitude at the same time (you need both to plot a point on a map, obviously).

You could easily rewrite the above code to only check one element instead of two.

The last part of the code snippet simply defines the polling interval variables such as "timeout" and the actual polling interval (all in milliseconds) :

       }
    })
  }

 },
     dataType: "json",
     complete: poll,
     timeout: 2000
 })
}, 3000);
})();

Pretty straightforward! Find the entire code snippet below , for your reference :

              (function poll() {
                setTimeout(function() {
                           $.ajax({
                                  url: "https://yoursite.com/request",
                                  headers: {
                                  'Content-Type': 'application/x-www-form-urlencoded'
                                  },
                                  type: "GET",
                                  success: function(data) {

                                  // check if null return (no results from API)
                                  if (data == null) {
                                        console.log('no data!');
                                  } else {
                                        $.each(data, function(index, element) {
                                               arrayName.push([element.whatever1, element.whatever2]);
                                               // cycle through internal array to see if match, or existing coordinates
                                               for (var i = 0; i < arrayName.length; i++) {
                                                    // check if new data received matches what is in internal array
                                                    if (arrayName[i][0] === element.whatever1 && arrayName[i][1] === element.whatever2) {
                                                        console.log('data received is already in array .. skipping');
                                                    } else {
                                                        console.log('new data!');
                                                    }
                                               }
                                         })
                                  }

                                  },
                                  dataType: "json",
                                  complete: poll,
                                  timeout: 2000
                                  })
                           }, 3000);
                })();

At Shift8, we cater to all sorts of businesses in and around Toronto from small, medium, large and enterprise projects. We are comfortable adapting to your existing processes and try our best to compliment communication and collaboration to the point where every step of the way is as efficient as possible.

Our projects are typically broken into 5 or 6 key “milestones” which focus heavily on the design collaboration in the early stages. We mock-up any interactive or unique page within your new website so that you get a clear picture of exactly how your design vision will be translated into a functional website.

Using tools like Basecamp and Redpen, we try to make the process simple yet fun and effective. We will revise your vision as many times as necessary until you are 100% happy, before moving to the functional, content integration and development phases of the project.

For the projects that are more development heavy, we make sure a considerable amount of effort is spent in the preliminary stages of project planning. We strongly believe that full transparency with a project development plan ensures that expectations are met on both sides between us and the client. We want to ensure that the project is broken into intelligent phases with accurate budgetary and timeline breakdowns.

Approved design mock-ups get translated into a browse-ready project site where we revise again and again until you are satisfied. Client satisfaction is our lifeblood and main motivation. We aren’t happy until you are.

Need Web Design?

Fill out the form to get a free consultation.

shift8 web toronto – 416-479-0685
203A-116 geary ave. toronto, on M6H 4H1, Canada
© 2023. All Rights Reserved by Star Dot Hosting Inc.

contact us
phone: 416-479-0685
toll free: 1-866-932-9083 (press 1)
email: sales@shift8web.com

Shift8 Logo