Search code examples
javascripthttpxmlhttprequestfirefox-ostcpsocket

Efficient HTTP streaming in Firefox OS


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.


Solution

  • 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!