Skip to content package
tiny-eventsource v3.0.5 Tiny EventSource simplifies server-sent events for API servers
node >=16 0 deps esm cjs types open source
overview readme
license BSD-3-Clause
deps 0
unpacked 12.1 kB
files 9
node >=16 $ npm install tiny-eventsource@3.0.5n npmREADME GitHub-flavored package documentation.
downloads Across all versions.
Download trend unavailable.
compatibility Runtime metadata.
versions Latest published May 19, 2026
maintainers Registry package owners.
install size Estimated unpacked size across 1 packages.
16.1 kB
Includes dependencies.
contents Tiny EventSource
Tiny EventSource simplifies server-sent events (SSE) for API servers. An EventEmitter-based abstraction over the EventSource API for streaming events to HTTP clients.
Installation npm install tiny-eventsource copy
Quick-Start import { eventsource } from "tiny-eventsource" ;
const stream = eventsource ({ ms: 2e4 });
stream. init (req, res);
stream. send ({ data: "hello" }); copy
Usage
Basic Express Example import { eventsource } from "tiny-eventsource" ;
import { STATUS_CODES } from "node:http" ;
const streams = new Map ();
export function stream (req, res) {
if (req. isAuthenticated ()) {
const id = req.user.id;
if ( ! streams. has (id)) {
streams. set (id, eventsource ({ ms: 2e4 }, "connected" ));
}
streams. get (id). init (req, res);
} else {
res.statusCode = 401 ;
res. writeHead (res.statusCode, { headers: { "cache-control" : "no-cache, must re-validate" } });
res. end (STATUS_CODES[res.statusCode]);
}
} copy
API
Constructor Creates an EventSource instance with optional messages to be transmitted on successful connection.
new EventSource ({event?: string, ms?: number, msg?: string}, ... msgs) copy Parameter Type Default Description config object|stringundefinedOptions object or initial event name string ...msgs string[][]Initial messages to send on connection
Returns: EventSource instance
eventsource() Factory function that creates and returns an EventSource instance.
eventsource ({event?: string, ms?: number, msg?: string}, ... msgs) copy Parameter Type Default Description ...args string|object— Config options and initial messages
Returns: EventSource instance
init(req, res) Initializes the SSE stream, sets response headers, and starts the heartbeat if configured.
stream. init (req, res); copy Parameter Type Default Description req IncomingMessageundefinedHTTP incoming request — supports socket.setTimeout, setNoDelay, setKeepAlive res ServerResponseundefinedHTTP server response — sets Content-Type, Cache-Control, Connection headers
Returns: this (EventSource)
send(msg[, event, id]) Emits a data event that is transmitted to the client.
stream. send (data, event, id); copy Parameter Type Required Description data string|objectyes Message data — objects are JSON-stringified event stringno Custom event name (default: "message") id numberno Message ID for client reconnection
Returns: this (EventSource)
listenerCount(event) Returns the number of listeners registered for the given event.
stream. listenerCount ( "data" ); copy Parameter Type Required Description event stringyes Event name to count listeners for
Returns: number — listener count
setMaxListeners(n) Sets the maximum number of listeners for the instance. Pass 0 to disable the limit.
stream. setMaxListeners ( 0 ); copy Parameter Type Required Description n numberyes Maximum number of listeners; 0 means unlimited
Returns: this (EventSource)
stop() Stops the heartbeat interval if one is active.
Returns: this (EventSource)
Options Option Type Default Description eventstring"message"Event name for heartbeat/ping msnumber0Heartbeat interval in milliseconds; set > 0 to enable msgstring"ping"Message sent for heartbeat pings
Events
close Emitted when an EventSource request is closed or the client disconnects.
stream. on ( "close" , () => {
// cleanup
}); copy
License Copyright (c) 2023-2026 Jason Mulligan
Licensed under the BSD-3 License