Better SSE is a robust, dependency-less, and spec-compliant implementation of Server-Sent Events (SSE) written entirely in TypeScript. It provides a streamlined, framework-agnostic solution for pushing data from a server to clients over HTTP, making it an alternative to WebSockets for unidirectional data flows. Currently stable at version 0.16.1, the project demonstrates a consistent release cadence with updates addressing compatibility, new features like connection adapters and event batching, and continuous type improvements. Key differentiators include its full TypeScript support, extensive documentation, 100% test coverage, and compatibility across various Node.js frameworks and runtimes (e.g., Express, Hono, Fastify, Bun, Deno), operating directly over the HTTP protocol to reduce bandwidth and complexity compared to other real-time solutions.
npm install better-sseVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates setting up a basic Server-Sent Events endpoint using Express. It initializes a session for a new client connection, sends an initial greeting, and then periodically pushes updates until the client disconnects.
Review the documentation on 'Connection Adapters' if encountering issues with specific frameworks or runtimes. Update to v0.16.1 for fixes related to Node HTTP/1 and HTTP/2 compatibility issues in emulated environments.
Upgrade to better-sse v0.14.0 or newer to ensure correct ESM import behavior. For older versions, carefully check module resolution configurations (e.g., `"type": "module"` in `package.json`, `tsconfig.json` settings).
Upgrade to v0.16.1 or newer, which includes a fix for this crash by gracefully handling the absence of `setNoDelay`.
Update to v0.12.1 or later to benefit from enhanced TypeScript type inference and suggestions for event names, improving developer experience and reducing errors.
Ensure you are using ES module imports: `import { createSession } from 'better-sse';`. If in a CommonJS context, consider configuring your project to support ESM or using an older version if available (though newer versions are ESM-first).Ensure `res.setHeader()` calls for `Cache-Control`, `Content-Type`, and `Connection` are made *before* `createSession(req, res)` or any data is written to the response stream. This often means placing them early in your SSE route handler.
Implement proper session cleanup by listening to the `close` event on the `Session` object (`session.on('close', () => { /* cleanup resources */ });`) to stop sending events or clear timers specific to that client.No dependency data recorded yet.