eventsource-parser is a highly performant and source-agnostic streaming parser for Server-Sent Events (SSE) / EventSource data. It does not assume how the data stream is obtained, making it a versatile building block for various JavaScript environments like browsers, Node.js, and Deno. The current stable version is 3.0.7, with frequent releases addressing bug fixes, performance improvements, and ongoing maintenance. Key differentiators include its streaming nature, a `TransformStream` variant for modern environments, and its role as a low-level parser that can be integrated into custom networking stacks or clients, contrasting with higher-level clients like `eventsource-client` or Node.js polyfills like `eventsource`.
npm install eventsource-parserVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to create and use the `eventsource-parser` to process a simulated Server-Sent Events stream, handle parsed events, process retry interval directives, and catch parsing errors. It showcases the new callback-based API introduced in v3.
Migrate your parser initialization to `createParser({ onEvent, onRetry, onError })` and update type references from `ParsedEvent` to `EventSourceMessage`. Refer to the official migration guide for a complete overview.Ensure your Node.js environment is version 18 or higher. If you need to support older Node.js versions, you must stick to `eventsource-parser` v1.x.
Always call `parser.reset({ consume: true })` within a `finally` block or after confirming the stream's completion, especially when reusing a parser instance.For the `TransformStream` variant, always import from `eventsource-parser/stream` to ensure stability across patch versions and clarity on module intent. For the main parser, use named ESM imports.
Ensure you are passing an object with `onEvent`, `onRetry`, `onError` (and optionally `onComment`) callbacks to `createParser`, not a single function. For example: `createParser({ onEvent: (event) => {...} })`.Use a named import: `import { createParser } from 'eventsource-parser'`. For CommonJS environments, consider a tool like `esm` or ensure your build process correctly handles ESM imports. If passing a function directly, wrap it in an object for the `onEvent` callback: `createParser({ onEvent: myEventHandler })`.Ensure you import it as a type: `import type { EventSourceMessage } from 'eventsource-parser'`. If you are on an older version, use `ParsedEvent` instead.Upgrade your Node.js runtime to version 18 or newer, or downgrade `eventsource-parser` to v1.x if older Node.js versions are required.
No dependency data recorded yet.