SSE Server and Client
Streaming helpers are in @kortyx/stream and re-exported by kortyx.
Server: createStreamResponse
TS
import { createStreamResponse, type StreamChunk } from "kortyx";
async function* run(): AsyncGenerator<StreamChunk> {
yield { type: "status", message: "starting" };
yield { type: "message", content: "hello" };
yield { type: "done" };
}
export async function GET() {
return createStreamResponse(run());
}createStreamResponse serializes each chunk as:
TEXT
data: {json}\n\nand finishes with:
TEXT
data: [DONE]\n\nClient: readStream
TS
import { readStream } from "kortyx";
const response = await fetch("/api/chat");
for await (const chunk of readStream(response.body)) {
if (chunk.type === "text-delta") {
// update UI incrementally
}
}Default stream headers
createStreamResponse sets:
content-type: text/event-streamcache-control: no-cacheconnection: keep-alivex-accel-buffering: no