elysiajs / stream

Plugin for Elysia for streaming response and Server Sent Event
MIT License
15 stars 2 forks source link

Streaming chunks accumulating and/or being lost. #2

Closed emilianomon closed 11 months ago

emilianomon commented 11 months ago

I am having this issue, since I've migrated from Node to Bun, writing streams by hand or using this plugin.

The problem consists of streams accumulating chunks and displaying all at once OR chunks simply being lost.

Here is an example: PS: If I simply return the Stream object like it's said in the docs, Elysia returns a "jsonfied" version of new Stream(...), so... I had to tweak it a little.

const stream = new Stream(async (st) => {
  let count = 1;
  const interval = setInterval(() => {
    st.send({ count });
    count++;
    if(count > 50) {
      st.close();
      clearInterval(interval);
    }
  }, 10);
});

return new Response(stream.value, {
  headers: {
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
    'Content-Type': 'text/event-stream',
    'Transfer-Encoding': 'chunked',
  },
  status: 200,
});

The code above works with 250ms timeouts or higher, but won't with faster frequencies.

Since the project I am working leverages gen AI, this timeout won't deliver an acceptable UX.

Assuming the snippet is within a handler, what could be going wrong?

emilianomon commented 11 months ago

I did not know about this, but it seems like Postman bugs out if you request a high frequency stream multiple times without clearing the response's textarea.

To work around it just clear the response and request again and the problem must be gone.

image image

Btw: I do not remember this behaviour from Postman when using Node. If anyone has had this issue before, leave a comment.