2

I need to handle an infinite HTTP response (with Transfer-Encoding: chunked header).

This response contains a stream of images, so it must be handled as efficiently as possible.

XmlHttpRequest is not a solution here since it keeps all the reply in memory. Plus, if reading ArrayBuffer, the response isn't populated before the end of streaming, which means never here.

So, since I am under Firefox OS, the TCPSocket API seems to be my only hope.

I already started to implement a dirty HTTP stack (here and here), getting inspiration from the IMAP/SMTP implementations but it is still very slow.

So, two questions:

  1. Is it worth spending time on this, or did I miss something easier?

  2. If I want to implement it, what are the best practices not to foget about?

PS: I communicate with an external device, so changes on the server side are just not possible here.

Lithy
  • 817
  • 12
  • 23
  • An example of a similar infinite request can be found in the [Twitter API](https://dev.twitter.com/streaming/overview/processing) – Lithy Sep 20 '15 at 22:53

1 Answers1

1

As stated by the XMLHttpRequest doc on MDN, Firefox actually makes available extra responseType values (and so does Firefox OS) for streaming data, like moz-chunked-arraybuffer.

var xhr = new XMLHttpRequest({ mozSystem: true });
xhr.responseType = "moz-chunked-arraybuffer";
xhr.open('GET', deviceStreamingUrl);
xhr.addEventListener('progress', event => {
  processChunk(xhr.response);
});
xhr.send();

Thanks to fabrice on #fxos@irc.mozilla.org!

Lithy
  • 817
  • 12
  • 23