5

I discoverd recently chunked response. I agree that most of the time we want to work on a full response. But what if I want to work on a chunked response.

How would i do this with the $http service??

benzen
  • 6,204
  • 4
  • 25
  • 37
  • You'll likely need to create your own service with $httpInterceptor. Do you have a sample of the chunked responses? – Ben Lesh Nov 01 '12 at 15:09
  • typically it will be json object separated by \n Do you have exemple of such use of $httpInterceptor ? – benzen Nov 01 '12 at 15:42
  • Excuse me, I misspoke, not necessarily a service, but something that did the magic of putting the multiple responses together. ... but it sounds like you have *one* response that has many JSON parts in it? – Ben Lesh Nov 01 '12 at 17:02
  • Exactly. My idea is to use the each json object as soon as they are recieved. I ad this idea, because the list of object is long to produce, and to retrieve. – benzen Nov 01 '12 at 17:10
  • Why not put each chuck into a master JSON object, using it like a dictionary? { 'part1': {}, 'part2': {} } ... then you could just write a service or something that got that for you and split it into it's appropriate pieces. – Ben Lesh Nov 01 '12 at 17:18
  • Sorry i wasn't clear, the chunks are slow to produce on the server side. Each json object is stored in s separate db row. And retreiving each one could take long depending on the number of record. So i want to send each record as soon as it's retreived from the db. – benzen Nov 01 '12 at 17:49
  • Sounds like a job for multiple asynchronous requests. Must it be done in one request? Wouldn't it kill scalability to block on your webserver while one mega response was being piped out? – Ben Lesh Nov 01 '12 at 19:20
  • Your're right, this would be more scalable in a general case. But with the usage of this app, i will be the only one to use this app. So it's more an exercice than anything else. The way i think about this woul be something like https://dev.twitter.com/docs/streaming-apis – benzen Nov 02 '12 at 12:22

2 Answers2

4

You can define a function with the angularjs promise $q wrapping the XMLHttpRequest.

var chunkedRequestWithPromise = function () {
  var deferred = $q.defer();
  var xhr = new XMLHttpRequest()
  xhr.open("GET", 'https://yoururl.com/chunked', true)
  xhr.onprogress = function () {
    deferred.notify(xhr.responseText);
  }
  xhr.onreadystatechange = function (oEvent) {
    if (xhr.readyState === 4) {
        if (xhr.status === 200) {
            deferred.resolve('success');
        } else {
            deferred.reject(xhr.statusText);
        }
    }
  };
  xhr.send();
  return deferred.promise;
};

and use it:

chunkedRequestWithPromise().then(successFn,errorFn,notifyFn);
Stef Chäser
  • 1,911
  • 18
  • 26
1

I've found a way to do something that looks correct to me http://www.igvita.com/2011/08/26/server-sent-event-notifications-with-html5/

Of course there is also the websocket api, but it seams heavy for my uses

Another possibility would be the write another http service which would give control over the http response

benzen
  • 6,204
  • 4
  • 25
  • 37