Accessing partial response using AJAX or WebSockets?

Wanted to post this as a comment but the comment textarea is a bit restrictive

Does the server send the data in chunks at the moment or is it one continuous stream? If you add a handler to the xmlHttpRequestInstance.onreadystatechange how often does it get triggered? If the xmlHttpRequestInstance.readystate has a value of 3 then you can get the current value of the xmlHttpRequestInstance.responseText and keep a track of the length. The next time the readystate changes you can get the most recent data by getting all new data starting at that point.

I’ve not tested the following but hopefully it’s clear enough:

var xhr = new XMLHttpRequest();
var lastPos = 0;
xhr.onreadystatechange = function() {
  if(xhr.readystate === 3) {
    var data = xhr.responseText.substring(lastPos);
    lastPos = xhr.responseText.length;

    process(data);
  } 
};
// then of course do `open` and `send` :)

This of course relies on the onreadystatechange event firing. This will work in IE, Chrome, Safari and Firefox.

Leave a Comment