Registry / messaging / fetch-sse

fetch-sse

JSON →
library1.2.1jsnpmunverified

A lightweight library (v1.2.1) that provides an easy API for consuming Server-Sent Events (SSE) using the Fetch API, with full support for browsers and Node.js (>=18). It offers a single main function `fetchEventData` with familiar fetch-like options, including custom headers, method, body, signal, and event callbacks (onMessage, onOpen, onClose, onError). Key differentiators: no dependencies, TypeScript-first, supports POST requests and custom status handling via `skipStatusCheck`. Regular releases, actively maintained on GitHub.

npm install fetch-sse
INSTALL
IMPORT
SIG · FETCH-SSE
F
fetch-sse
messagingjavascriptv1.2.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

fetchEventData
✓ import { fetchEventData } from 'fetch-sse'
✗ import fetchEventData from 'fetch-sse'
Only named export is available; no default export.
ServerSentEvent
✓ import { ServerSentEvent } from 'fetch-sse'
TypeScript type for parsed SSE events.
IFetchOptions
✓ import { IFetchOptions } from 'fetch-sse'
TypeScript interface for options parameter.

Demonstrates basic usage of fetchEventData with POST request, Bearer token auth, and event callbacks. Includes abort signal and environment variable for URL and API key.

import { fetchEventData } from 'fetch-sse'; import process from 'process'; const url = process.env.SSE_URL ?? 'https://example.com/sse'; const token = process.env.API_KEY ?? ''; const controller = new AbortController(); await fetchEventData(url, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}` }, data: { query: 'latest' }, signal: controller.signal, onMessage: (event) => { console.log('Received event:', event); }, onOpen: (res) => { console.log('Stream opened, status:', res?.status); }, onError: (err) => { console.error('Error:', err); }, onClose: () => { console.log('Stream closed'); } }); // Abort after 10 seconds setTimeout(() => controller.abort(), 10000);
Debug
Known issues
gotchaDefault status check rejects non-2xx responses; onOpen is not called for non-2xx unless skipStatusCheck is true.
fix
Set skipStatusCheck: true to handle custom status codes manually.
affects: >=1.0.0
gotchaThe `data` option is automatically JSON-stringified if it is an object; to send a string body directly, pass a string (e.g., 'plain text').
fix
Use `data: JSON.stringify({...})` if you need explicit control, or pass a string as is.
affects: >=1.0.0
deprecatedNo deprecated features identified in current version.
fix
N/A
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: fetchEventData is not a function
Using default import instead of named import.
fix
Use `import { fetchEventData } from 'fetch-sse'` instead of `import fetchEventData from 'fetch-sse'`.
ReferenceError: TextDecoder is not defined
Running in an older Node.js version (<18) or browser without TextDecoder polyfill.
fix
Upgrade Node.js to >=18 or install a polyfill like `fast-text-encoding`.
Error: Event stream returned non-2xx status code
The server responded with a status code outside 200-299 and skipStatusCheck is not enabled.
fix
Add `skipStatusCheck: true` to options if you want to handle non-2xx responses in onOpen.
Upgrade
Version history
1.2.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
14 hits · last 30 days
node
12
OpenAI (training)
1
Resources
fetch-sse — npm install fetch-sse · libregistry