Next.js Discord

Discord Forum

How to stream Response with StreamingTextResponse being deprecated?

Unanswered
West African Crocodile posted this in #help-forum
Open in Discord
West African CrocodileOP
I want to stream the following response from ma Route Handler to my UI, but I am only able to collect all chunks asnc beforehand prior sending it all at once. I feel like streamText() is not an alternative to StreamingTextResponse, if you are not particulary interested in using ai.

  const chunks = [];
  for await (const chunk of stream) {
    console.log(chunk);
    chunks.push(chunk);
  }

  return new Response(JSON.stringify(chunks), {
    headers: {
      Connection: "keep-alive",
      "Content-Encoding": "none",
      "Cache-Control": "no-cache, no-transform",
      "Content-Type": "text/event-stream; charset=utf-8",
    },
  });

0 Replies