I am streaming some data using Node.js / Express.js Server-Sent Events:
app.get('/', async (req, res) => {
// Implementaiton detail doesn't matter, writing some pieces over some period of time
await getSomeAsyncData((chunk) => {
console.log("Writing a chunk to client, size", chunk.length);
res.write(chunk);
});
res.end();
}
On the client-side, I am using a simple fetch
call:
// Fetching the endpoint above
const req = await fetch(`https://example.com/my-endpoint`);
let done = false;
while (!done) {
let result = await req.body.getReader().read();
done = result.done;
if (result.value) {
console.log("Received chunk of length", result.value.length);
}
}
As a result, the full data (118708 bytes in this case) is sent, but the chunks are split in a different way, making them impossible to parse. The logs on the server side read:
Writing a chunk to client, size 36128
Writing a chunk to client, size 36200
Writing a chunk to client, size 33648
Writing a chunk to client, size 12732
And the logs on the client read:
Received chunk of length 68786
Received chunk of length 36864
Received chunk of length 12288
Received chunk of length 770
Are the messages not supposed to be sent in exactly the same way? Is there something that could be making them recombined and split differently?