Consume native streams
Read incremental SSE events with an explicit lifetime and provider-specific event interpretation.
On this page
Events are native data
import type { NativeSSEEvent } from '@belweave/search-sdk';
export async function consume(events: AsyncIterable<NativeSSEEvent>) {
for await (const event of events) {
console.log(event.event, event.id, event.data);
// Interpret data and terminal/error events using the provider contract.
// Do not assume every event is JSON or that HTTP 200 means job success.
}
}An SSE operation resolves to { data, metadata } after response headers. Iterate response.data exactly once. event.data is a string; event, id, and retry may be present. The parser handles split UTF-8 and CR/LF boundaries, but provider event schemas still require interpretation.
Own the stream lifetime
- Pass an AbortSignal to the operation and abort it when the consumer is done.
- Breaking a for-await loop cancels the response body.
- The operation deadline covers the full stream lifetime, including periods when your consumer is idle.
- Response-byte limits apply to the stream as a whole, rather than restarting for each event.
No automatic reconnect or replay
The SSE retry field is preserved as event data. It does not enable EventSource reconnect behavior. An incomplete final event is discarded under the parser's SSE framing rules. Metadata attempts describe connection setup; later iteration errors do not become new HTTP attempts.