Registry / http-networking / connect-sse

connect-sse

JSON →
library1.2.0jsnpmunverified

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-sse
INSTALL
IMPORT
SIG · CONNECT-SSE
C
connect-sse
http-networkingjavascriptv1.2.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

sse
const sse = require('connect-sse')();
import sse from 'connect-sse'; import { sse } from 'connect-sse';
connect-sse is a CommonJS module and exports a function which must be called to return the middleware. It does not support ES Modules.

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.

const express = require('express'); const sse = require('connect-sse')(); const app = express(); app.get('/events', sse, (req, res) => { // Set up an interval to send events every 2 seconds let counter = 0; const intervalId = setInterval(() => { if (res.finished) { // Client disconnected, clean up interval clearInterval(intervalId); return; } const eventData = { timestamp: new Date(), counter: counter++ }; res.json(eventData, 'update'); // Send a named event 'update' res.json(`Plain text message ${counter}`); // Send a default 'message' event }, 2000); // Ensure the connection closes if the client disconnects req.on('close', () => { clearInterval(intervalId); console.log('Client disconnected, SSE stream closed.'); }); console.log('Client connected for SSE stream.'); }); app.get('/', (req, res) => { res.send(` <!DOCTYPE html> <html> <head><title>SSE Test</title></head> <body> <h1>Server-Sent Events Demo</h1> <div id="output"></div> <script> const eventSource = new EventSource('/events'); eventSource.onmessage = (event) => { const p = document.createElement('p'); p.textContent = `Default message: ${event.data}`; document.getElementById('output').appendChild(p); }; eventSource.addEventListener('update', (event) => { const data = JSON.parse(event.data); const p = document.createElement('p'); p.textContent = `Update event: Timestamp ${data.timestamp}, Counter ${data.counter}`; document.getElementById('output').appendChild(p); }); eventSource.onerror = (error) => { console.error('EventSource failed:', error); eventSource.close(); }; console.log('Client-side EventSource initialized.'); </script> </body> </html> `); }); const PORT = process.env.PORT || 3000; app.listen(PORT, () => { console.log(`SSE server listening on http://localhost:${PORT}`); console.log(`Visit http://localhost:${PORT} in your browser to see events.`); });
Debug
Known issues
breakingThe `connect-sse` package is effectively abandoned, with its last known update over a decade ago (2013). This means it receives no security patches, bug fixes, or compatibility updates for newer Node.js versions or web standards.
fix
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.
affects: >=1.0.0
gotcha`connect-sse` is a CommonJS-only module. Attempting to import it using ES module syntax (`import ... from 'connect-sse'`) will result in a runtime error.
fix
Always use the CommonJS `require()` syntax: `const sse = require('connect-sse')();`.
affects: >=1.0.0
gotchaThe SSE specification (and thus `connect-sse`) is designed for unidirectional data flow (server-to-client only). If your application requires bi-directional communication, Server-Sent Events are not suitable.
fix
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.
affects: >=1.0.0
gotchaServer-Sent Events transmit data as UTF-8 encoded text only. Binary data must be encoded (e.g., Base64) before transmission, which adds overhead.
fix
If efficient binary data transfer is critical, WebSockets are a more appropriate technology. For text-based data, SSE works well.
affects: >=1.0.0
gotchaWhen not using HTTP/2, SSE connections may be limited to six concurrent connections per browser/domain (a browser-level limitation). While HTTP/2 mitigates this by allowing multiplexing, legacy HTTP/1.1 environments can still hit this ceiling, impacting scalability for many clients.
fix
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.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: sse is not a function
The `require('connect-sse')` statement returns a function that must be immediately invoked to get the middleware. Forgetting the `()` after the require call.
fix
Change `const sse = require('connect-sse');` to `const sse = require('connect-sse')();`.
Cannot use import statement outside a module
Attempting to use ES module `import` syntax (`import sse from 'connect-sse'`) in a Node.js environment configured for CommonJS, or for a package that only provides CommonJS exports.
fix
Use the CommonJS `require()` syntax: `const sse = require('connect-sse')();`.
ERR_STREAM_WRITE_AFTER_END
Attempting to write to the response stream after the client has disconnected or the stream has been implicitly or explicitly ended. This often happens if cleanup (like `clearInterval` for sending data) is not properly handled on client disconnection.
fix
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.
Upgrade
Version history
1.2.0latest on npm
Audit
Dependencies
connectrequiredThis package is a middleware for the connect framework, which forms its core functionality.
Agent activity
15 hits · last 30 days
node
12
Amazon
1
OpenAI (training)
1
Resources
connect-sse — npm install connect-sse · libregistry