Hi, is it possible to stream data from a Next.js api route when my SST stack is deployed to prod? This is my first time using SST, so it’s not clear to me what the limitations are.
I have created a simple Next.js site that contains an api route (app router). The api route accepts a payload via POST with some data. Then it calls the OpenAI api to retrieve a response. I want to stream that response back to my client component (similar to the ChatGPT interface). I have this working when running my SST stack in dev, but once I deploy it to prod, the response is no longer streamed, it just returns the full data once complete. Here is the code that does the stream/response:
const headers = new Headers({
"Content-Type": "text/plain; charset=utf-8",
"Transfer-Encoding": "chunked",
});
const stream = new ReadableStream({
async start(controller) {
try {
const openaiStream = await openai.chat.completions.create({
model: model.id,
messages,
stream: true,
});
for await (const chunk of openaiStream) {
const content = chunk.choices[0]?.delta?.content || "";
controller.enqueue(new TextEncoder().encode(content));
}
controller.close();
} catch (error) {
console.error("Error calling OpenAI API:", error);
controller.error(error);
}
},
});
return new NextResponse(stream, { headers });