parseSSEStream
parseSSEStream(
response,options):AsyncGenerator<SSEEvent,void,unknown>
Defined in: stream-utils.ts:114
Parse Server-Sent Events (SSE) stream from a fetch response.
This async generator reads a streaming response body conforming to the Server-Sent Events specification and yields parsed event objects. It handles SSE event delimiters (double newlines), parses event fields (event, data, id, retry), and buffers incomplete events. Supports cancellation via abort signal.
Parameters
Section titled “Parameters”response
Section titled “response”Response
Fetch Response object with SSE formatted body
options
Section titled “options”Configuration including optional abort signal
signal?
Section titled “signal?”AbortSignal
Returns
Section titled “Returns”AsyncGenerator<SSEEvent, void, unknown>
Yields
Section titled “Yields”Parsed SSE events with event type, data, id, and retry fields
Throws
Section titled “Throws”If response body is null
Example
Section titled “Example”const response = await fetch('/api/chat');const abortController = new AbortController();
for await (const event of parseSSEStream(response, { signal: abortController.signal})) { console.log('Event:', event.event, 'Data:', event.data); if (event.data === '[DONE]') break;}