express-sse (from `dpskvn/express-sse`) is a lightweight Express.js middleware designed for easily implementing Server-Sent Events (SSE) in Node.js applications. It provides a straightforward API to send real-time, unidirectional updates from the server to connected clients over a standard HTTP connection, making it suitable for dashboards, live feeds, and notification systems. The current stable version is `1.0.0`, released on April 17, 2025, indicating active development and maintenance. Its primary differentiator is its simplicity and focus on one-way communication, which often makes it a more suitable and simpler alternative to WebSockets for server-to-client data streaming scenarios. It handles the necessary HTTP headers and connection management for SSE, allowing developers to focus on sending data without extensive boilerplate.
npm install express-sseVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to set up an Express.js server using `express-sse` to stream real-time updates. It initializes SSE with some optional initial data, then sends periodic 'time-update' events. It also shows how to send custom-named events (like 'error') and includes client-side JavaScript for consuming the stream using the browser's `EventSource` API.
Configure your reverse proxy/load balancer with 'no-buffering' settings for SSE endpoints. For Nginx, add `proxy_buffering off; proxy_cache off; proxy_http_version 1.1; proxy_set_header Connection '';` to your location block.
Exclude SSE routes from compression middleware. For example, if using `compression`, apply it conditionally: `app.use(compression({ filter: shouldCompress }))` and define `shouldCompress` to return `false` for your SSE paths.Be mindful of client-side connection limits. Consider consolidating data streams where possible, using a single SSE connection for multiple types of events, or leveraging HTTP/2 which allows multiplexing over a single TCP connection. For applications requiring many simultaneous connections, WebSockets might be a better fit.
When sending multi-line content or data that might contain newlines (e.g., JSON strings, markdown), ensure that each line in your data is prefixed with `data:` and ends with `\n`, with the final data line followed by two `\n\n` to properly terminate the event. Alternatively, JSON-serialize your data and then stringify it, ensuring any internal newlines are escaped as `\n` within the JSON string itself (e.g., `data: {"text": "line1\nline2"}\n\n`).Ensure `res.flushHeaders()` is called after setting SSE-specific headers (handled by `express-sse.init`). Check for proxy buffering or compression middleware interfering with the stream and disable them for the SSE route. For `express-sse`, ensure `sse.init` is correctly applied to the route.
If your project uses CommonJS (no `"type": "module"` in `package.json`), use `const SSE = require('express-sse');`. If using ES modules, ensure your Node.js environment supports them (Node.js >= 12 with `"type": "module"` or via Babel/TypeScript compilation), and use `import SSE from 'express-sse';`.Verify that `express-sse` is correctly installed (`npm install express-sse`) and that the `SSE` class is being imported/required as `const SSE = require('express-sse');` or `import SSE from 'express-sse';` before attempting to call `new SSE()`.