express-sse-middleware is an npm package providing a robust Express.js middleware for implementing Server-Sent Events (SSE) in Node.js applications. It simplifies the process of pushing real-time updates from a server to clients over a single HTTP connection, making it suitable for dashboards, chat applications, and live data feeds. The current stable version is 3.0.3, released after a significant v3.0.0 update which transitioned the package to an ES Module (ESM) first architecture. While a strict release cadence isn't explicitly defined, the package appears actively maintained given recent major version bumps and ongoing development. Its primary differentiator is its straightforward integration into existing Express applications by extending the `Response` object with a dedicated `sse()` method, allowing developers to easily send data, custom events, and IDs to connected clients. It also offers an `EventBuilder` for more structured event creation, though direct string sending is fully supported and often sufficient for simple use cases.
npm install express-sse-middlewareVerified import paths — ran on the pinned version, not inferred.
Sets up an Express server to push continuously incrementing numbers to clients every second over a Server-Sent Events connection, demonstrating basic usage and crucial client disconnect cleanup.
Ensure your `package.json` includes `"type": "module"` and use `import` statements for all modules. For TypeScript, ensure `"module": "NodeNext"` or `"ESNext"` is configured in `tsconfig.json`.
Attach a 'close' event listener to the `req` object (`req.on('close', () => { /* cleanup code */ });`) to ensure all allocated resources for that specific client connection are released.Install and configure the `cors` middleware (e.g., `npm install cors`) in your Express application. Add `app.use(cors({ origin: 'http://your-client-domain.com' }));` before your SSE route to allow the connection.Ensure your SSE route logic is stateless for new connections, or can gracefully re-establish context for a client if necessary. Design client-side to use `EventSource` which handles retries, or implement custom retry logic.
Ensure `app.use(sseMiddleware);` is called at an appropriate point in your Express application setup, typically before any routes that utilize SSE features.
Update your import statements to use ES Module syntax (e.g., `import { sseMiddleware } from 'express-sse-middleware';`) and ensure your `package.json` specifies `"type": "module"`.Install and use the `cors` middleware in your Express application: `npm install cors` then `import cors from 'cors'; app.use(cors({ origin: 'http://your-client-app-domain.com' }));`.No dependency data recorded yet.