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\n

and finishes with:

TEXT
data: [DONE]\n\n

Client: 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-stream
  • cache-control: no-cache
  • connection: keep-alive
  • x-accel-buffering: no