connect-sse is a middleware designed for `connect` (and compatible frameworks like Express) to facilitate the implementation of Server-Sent Events (SSE). It enables servers to push one-way event streams to clients over a persistent HTTP connection, which is commonly used for real-time updates such as live news feeds, stock prices, notifications, and interactive dashboards. The package is currently at version 1.2.0. Based on its last commit in 2013 and a `node` engine requirement of `>=0.10.0`, the project appears to be abandoned and no longer actively maintained or developed. Its primary differentiator is providing a simple, high-level abstraction over raw HTTP streaming for SSE, integrating directly into the `connect` middleware stack. Unlike WebSockets, SSE is strictly unidirectional (server-to-client only), simpler to set up for push notifications, and operates entirely over standard HTTP/1.1, with benefits from HTTP/2 multiplexing for improved efficiency. It lacks native support for modern ESM imports or TypeScript.
npm install connect-sseVerified import paths — ran on the pinned version, not inferred.
This quickstart sets up an Express server using connect-sse to stream real-time JSON and plain text events to a client every two seconds. It includes a basic HTML client that uses the native EventSource API to listen for both default 'message' events and a custom 'update' event, displaying them dynamically in the browser.
Consider migrating to actively maintained SSE libraries like `better-sse` (npm: better-sse) or implementing SSE directly using native Node.js HTTP response manipulation, especially for new projects or applications requiring ongoing support.
Always use the CommonJS `require()` syntax: `const sse = require('connect-sse')();`.For bi-directional communication, consider using WebSockets or other full-duplex communication protocols. SSE is optimized for server-push scenarios like live feeds or notifications.
If efficient binary data transfer is critical, WebSockets are a more appropriate technology. For text-based data, SSE works well.
Ensure your server infrastructure supports HTTP/2 to leverage multiplexing for better scalability of SSE connections. Be mindful of this limitation in HTTP/1.1 environments and plan accordingly for a high number of clients.
Change `const sse = require('connect-sse');` to `const sse = require('connect-sse')();`.Use the CommonJS `require()` syntax: `const sse = require('connect-sse')();`.Implement robust disconnection handling using `req.on('close', ...)` to stop sending events and clean up resources (e.g., clear `setInterval` timers) as soon as the client connection is terminated.