sse.js: Server-Sent Events for Node.js
raw JSON →The `sse` package (einaros/sse.js) provides a server-side implementation of the HTML5 Server-Sent Events (SSE) specification for Node.js applications. SSE enables a server to push data to web pages over HTTP, facilitating real-time communication without the overhead of WebSockets for unidirectional data flows. Currently at version `0.0.8`, this package is considered abandoned, with its last release dating back approximately eight years (published December 2017, but copyright 2011) and minimal activity on its GitHub repository. Its primary differentiator was offering a straightforward, low-level API to integrate SSE directly with Node.js's native HTTP server, abstracting the complexities of managing event streams and client connections. Given its age, it relies exclusively on CommonJS and is likely incompatible with modern Node.js versions and ES module adoption. Users seeking SSE functionality in current Node.js environments should consider actively maintained alternatives such as `@fastify/sse`, `sse-channel`, or `better-sse` due to potential compatibility, security, and feature limitations.
Common errors
error Error: Cannot find module 'sse' ↓
npm install sse to ensure the package is available. If using ES modules, rewrite your import to const SSE = require('sse'); in a CommonJS-compatible file, or switch to a modern SSE library with ESM support. error TypeError: Cannot read properties of undefined (reading 'on') ↓
http.createServer correctly returns a server instance and that the server is properly listening before passing it to new SSE(server). Given the package's age, fundamental incompatibilities with modern Node.js http module APIs are likely; consider migrating to an alternative SSE library. Warnings
breaking This package is designed for very old Node.js versions (>=0.4.0) and is largely incompatible with modern Node.js runtimes due to significant API changes and lack of maintenance. Direct usage may lead to runtime errors or unexpected behavior on current Node.js versions. ↓
gotcha The `sse` package is unmaintained and marked as inactive/discontinued. This poses significant security risks as it will not receive updates for vulnerabilities or bug fixes. Its codebase has not been audited for modern security concerns. ↓
gotcha The package exclusively supports CommonJS (`require()`) syntax. It does not provide ES module exports and will cause runtime errors if `import` statements are used, making integration into modern JavaScript projects cumbersome. ↓
Install
npm install sse yarn add sse pnpm add sse Imports
- SSE wrong
import { SSE } from 'sse'; import SSE from 'sse';correctconst SSE = require('sse');
Quickstart
var SSE = require('sse')
, http = require('http');
var server = http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('okay');
});
server.listen(8080, '127.0.0.1', function() {
console.log('Server listening on http://127.0.0.1:8080');
var sse = new SSE(server);
sse.on('connection', function(client) {
console.log('Client connected. Sending initial message...');
client.send('hi there!');
// Example: Send a message every 3 seconds
let counter = 0;
const interval = setInterval(() => {
client.send(`Message from server: ${counter++}`);
}, 3000);
client.on('close', () => {
console.log('Client disconnected.');
clearInterval(interval);
});
});
});
// Client-side JavaScript (for browser to connect):
// var es = new EventSource("/sse");
// es.onmessage = function (event) {
// console.log("Received: " + event.data);
// };