This package provides a WhatWG/W3C compliant EventSource client, enabling applications to consume Server-Sent Events (SSE) in both Node.js and modern browser environments. The current stable version is 4.1.0, with regular updates and major releases approximately every six months, indicating active maintenance. It aims for a minimal implementation closely adhering to the specification, distinguishing itself from alternatives like `eventsource-client` which offer a more flexible API. It supports Node.js versions 20 and higher, along with a wide range of modern browsers, Deno, and Bun, by leveraging standard web APIs like `fetch`, `ReadableStream`, and `TextDecoder`. The library also provides a mechanism to override the `fetch` implementation, offering greater control in diverse runtime environments. TypeScript types are shipped directly with the package, requiring appropriate `tsconfig.json` setup for correct type inference depending on the target environment.
npm install eventsourceVerified import paths — ran on the pinned version, not inferred.
Demonstrates connecting to an SSE endpoint, listening for various event types, handling errors, and explicitly closing the connection.
Upgrade your Node.js environment to version 20 or newer. For older Node.js versions, use `eventsource@^3.0.0` or `eventsource@^2.0.0` depending on requirements.
Update your TypeScript code to use `EventSourceFetchInit` instead of `FetchLikeInit` for fetch initialization options.
Change your import statements from `import EventSource from 'eventsource'` to `import { EventSource } from 'eventsource'`. If targeting browsers without a bundler, consider using an older major version (2.x).To customize fetch behavior (e.g., add headers, use proxies, configure HTTPS agents), pass a custom `fetch` function to the `EventSource` constructor's options, wrapping your desired `fetch` implementation.
For browser environments, ensure your `tsconfig.json` includes `"lib": ["dom"]`. For Node.js, ensure `@types/node` is installed (version 18 or higher) and correctly configured in your `tsconfig.json`.
Configure your `tsconfig.json` to include the appropriate `lib` entry (e.g., `"lib": ["dom"]` for browsers) or ensure `@types/node` (>=18) is installed for Node.js environments.
Configure your `tsconfig.json` to include the appropriate `lib` entry (e.g., `"lib": ["dom"]` for browsers) or ensure `@types/node` (>=18) is installed for Node.js environments.
Configure your `tsconfig.json` to include the appropriate `lib` entry (e.g., `"lib": ["dom"]` for browsers) or ensure `@types/node` (>=18) is installed for Node.js environments.
For versions `>=3.0.0`, use `import { EventSource } from 'eventsource';`. If you must use CommonJS, consider transpiling or using an older major version (2.x) that supported CommonJS default exports directly.No dependency data recorded yet.