{"id":17552,"library":"connect-sse","title":"Connect SSE Middleware","description":"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.","status":"abandoned","version":"1.2.0","language":"javascript","source_language":"en","source_url":"https://github.com/andrewrk/connect-sse","tags":["javascript","connect","sse","middleware","eventsource","event","source","server","sent"],"install":[{"cmd":"npm install connect-sse","lang":"bash","label":"npm"},{"cmd":"yarn add connect-sse","lang":"bash","label":"yarn"},{"cmd":"pnpm add connect-sse","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"This package is a middleware for the connect framework, which forms its core functionality.","package":"connect","optional":false}],"imports":[{"note":"connect-sse is a CommonJS module and exports a function which must be called to return the middleware. It does not support ES Modules.","wrong":"import sse from 'connect-sse';\nimport { sse } from 'connect-sse';","symbol":"sse","correct":"const sse = require('connect-sse')();"}],"quickstart":{"code":"const express = require('express');\nconst sse = require('connect-sse')();\n\nconst app = express();\n\napp.get('/events', sse, (req, res) => {\n  // Set up an interval to send events every 2 seconds\n  let counter = 0;\n  const intervalId = setInterval(() => {\n    if (res.finished) {\n      // Client disconnected, clean up interval\n      clearInterval(intervalId);\n      return;\n    }\n    const eventData = { timestamp: new Date(), counter: counter++ };\n    res.json(eventData, 'update'); // Send a named event 'update'\n    res.json(`Plain text message ${counter}`); // Send a default 'message' event\n  }, 2000);\n\n  // Ensure the connection closes if the client disconnects\n  req.on('close', () => {\n    clearInterval(intervalId);\n    console.log('Client disconnected, SSE stream closed.');\n  });\n\n  console.log('Client connected for SSE stream.');\n});\n\napp.get('/', (req, res) => {\n  res.send(`\n    <!DOCTYPE html>\n    <html>\n    <head><title>SSE Test</title></head>\n    <body>\n      <h1>Server-Sent Events Demo</h1>\n      <div id=\"output\"></div>\n      <script>\n        const eventSource = new EventSource('/events');\n\n        eventSource.onmessage = (event) => {\n          const p = document.createElement('p');\n          p.textContent = `Default message: ${event.data}`;\n          document.getElementById('output').appendChild(p);\n        };\n\n        eventSource.addEventListener('update', (event) => {\n          const data = JSON.parse(event.data);\n          const p = document.createElement('p');\n          p.textContent = `Update event: Timestamp ${data.timestamp}, Counter ${data.counter}`;\n          document.getElementById('output').appendChild(p);\n        });\n\n        eventSource.onerror = (error) => {\n          console.error('EventSource failed:', error);\n          eventSource.close();\n        };\n\n        console.log('Client-side EventSource initialized.');\n      </script>\n    </body>\n    </html>\n  `);\n});\n\nconst PORT = process.env.PORT || 3000;\napp.listen(PORT, () => {\n  console.log(`SSE server listening on http://localhost:${PORT}`);\n  console.log(`Visit http://localhost:${PORT} in your browser to see events.`);\n});","lang":"javascript","description":"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."},"warnings":[{"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.","message":"The `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.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Always use the CommonJS `require()` syntax: `const sse = require('connect-sse')();`.","message":"`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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"The 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"If efficient binary data transfer is critical, WebSockets are a more appropriate technology. For text-based data, SSE works well.","message":"Server-Sent Events transmit data as UTF-8 encoded text only. Binary data must be encoded (e.g., Base64) before transmission, which adds overhead.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"When 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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-23T00:00:00.000Z","next_check":"2026-07-22T00:00:00.000Z","problems":[{"fix":"Change `const sse = require('connect-sse');` to `const sse = require('connect-sse')();`.","cause":"The `require('connect-sse')` statement returns a function that must be immediately invoked to get the middleware. Forgetting the `()` after the require call.","error":"TypeError: sse is not a function"},{"fix":"Use the CommonJS `require()` syntax: `const sse = require('connect-sse')();`.","cause":"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.","error":"Cannot use import statement outside a module"},{"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.","cause":"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.","error":"ERR_STREAM_WRITE_AFTER_END"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}