If I send chunked data from my webapp, will the client receive each chunk intact?

406 views Asked by At

I'm writing a very basic webapp that displays data requested from a server. The server sends JSON "packets" of data as a chunked response.

I'm reading the data from the server using the Javascript fetch API in my webpage, via the ReadableStream API. As far as I can tell from experimenting, each chunk that I send from the server arrives at the client as a separate block of data. If I assume that, my client is straightforward:

      const response = await fetch("/server_api");
      const reader = response.body.getReader();

      while (true) {
        const {value, done} = await reader.read();
        if (done) break;
        // convert "value" from an array of bytes to a JS object
        // by parsing it as JSON
        obj = JSON.parse(new TextDecoder().decode(value))
        // process the object
      }

However, this will fail if a chunk from the server gets split between two reads, or if two chunks from the server get merged in a single read.

Is this a possibility that I need to worry about? If I do, my code (both server and client side) will need to get significantly more complex, so I'd prefer to avoid that if I can.

Note that I'm specifically talking here about HTTP chunked responses and how they interact with the Javascript fetch API, not about TCP or other levels of the network stack.

1

There are 1 answers

0
Paul Moore On

Answering my own question, I have triggered a case where two blocks of data sent by the server arrived at the client in a single read() result from the reader.

So yes, you do have to allow for the possibility that chunks sent by the server get merged. I haven't seen a case of a chunk getting split, so it's possible that might not happen, but the key point is that there is not a one to one correspondence between the chunks the server sends and the chunks the client receives.