fetchSSE is a fetch-based SSE consumer that supports any HTTP method, request bodies, AbortSignal, multiple event types, and returns an AsyncGenerator so you can use for await … of.
Use fetchSSE instead of streamSSE whenever you need POST requests, request bodies, cancellation, or multiple event types. streamSSE (EventSource-based) remains available for simple GET-only SSE.
POST with body and cancellation
import { StreamingHelper } from "bytekit";interface TokenChunk { token: string; done: boolean;}const ac = new AbortController();for await (const ev of StreamingHelper.fetchSSE<TokenChunk>( "/bff/ai/orchestrator-stream", { method: "POST", body: { prompt: "Explain DDD", model: "gpt-4o" }, signal: ac.signal, eventTypes: ["data", "log", "heartbeat"], })) { if (ev.event === "data") { const chunk = ev.data as TokenChunk; process.stdout.write(chunk.token); if (chunk.done) break; }}
Filter by event type
Pass eventTypes to restrict which SSE event types are yielded. Omit it to receive all events.
for await (const ev of StreamingHelper.fetchSSE<{ price: number }>( "https://api.example.com/stock/stream", { eventTypes: ["price-update"] })) { console.log(`Price: $${(ev.data as { price: number }).price}`);}
Legacy SSE with streamSSE
For simple GET endpoints where you don’t need POST, body, or for await, the EventSource-based API still works: