How to parse SSE events in Fetch API with parse-sse
When working with Server-Sent Events (SSE) response format in API, you have to handle lot of things especially if you’re using native Fetch API.
For example, you need handle buffering, chunk boundaries, SSE metadata (data:, event:, id:), multiple data: lines in one event, etc
This lightweight library abstracts it away without compromising spec.
How to get started?
You can install this from npm
npm install parse-sse
Usage
import {parseServerSentEvents} from 'parse-sse';
const response = await fetch('https://api.example.com/events');
for await (const event of parseServerSentEvents(response)) {
console.log(event.type); // Event type (default: 'message')
console.log(event.data); // Event data
console.log(event.lastEventId); // Last event ID (always present as string)
console.log(event.retry); // Retry interval in ms (if specified)
}
Reference
Happy parsing SSE!