| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Tiny EventSource simplifies server-sent events (SSE) for API servers. An EventEmitter-based abstraction over the EventSource API for streaming events to HTTP clients.
Table of Contentsnpm install tiny-eventsourceimport { eventsource } from "tiny-eventsource";
const stream = eventsource({ ms: 2e4 });
stream.init(req, res);
stream.send({ data: "hello" });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]);
}
}Creates an EventSource instance with optional messages to be transmitted on successful connection.
new EventSource({event?: string, ms?: number, msg?: string}, ...msgs)| Parameter | Type | Default | Description |
|---|---|---|---|
| config | object|string | undefined | Options object or initial event name string |
| ...msgs | string[] | [] | Initial messages to send on connection |
Returns: EventSource instance
Factory function that creates and returns an EventSource instance.
eventsource({event?: string, ms?: number, msg?: string}, ...msgs)| Parameter | Type | Default | Description |
|---|---|---|---|
| ...args | string|object | — | Config options and initial messages |
Returns: EventSource instance
Initializes the SSE stream, sets response headers, and starts the heartbeat if configured.
stream.init(req, res);| Parameter | Type | Default | Description |
|---|---|---|---|
| req | IncomingMessage | undefined | HTTP incoming request — supports socket.setTimeout, setNoDelay, setKeepAlive |
| res | ServerResponse | undefined | HTTP server response — sets Content-Type, Cache-Control, Connection headers |
Returns: this (EventSource)
Emits a data event that is transmitted to the client.
stream.send(data, event, id);| Parameter | Type | Required | Description |
|---|---|---|---|
| data | string|object | yes | Message data — objects are JSON-stringified |
| event | string | no | Custom event name (default: "message") |
| id | number | no | Message ID for client reconnection |
Returns: this (EventSource)
Returns the number of listeners registered for the given event.
stream.listenerCount("data");| Parameter | Type | Required | Description |
|---|---|---|---|
| event | string | yes | Event name to count listeners for |
Returns: number — listener count
Sets the maximum number of listeners for the instance. Pass 0 to disable the limit.
stream.setMaxListeners(0);| Parameter | Type | Required | Description |
|---|---|---|---|
| n | number | yes | Maximum number of listeners; 0 means unlimited |
Returns: this (EventSource)
Stops the heartbeat interval if one is active.
stream.stop();Returns: this (EventSource)
| Option | Type | Default | Description |
|---|---|---|---|
| event | string | "message" | Event name for heartbeat/ping |
| ms | number | 0 | Heartbeat interval in milliseconds; set > 0 to enable |
| msg | string | "ping" | Message sent for heartbeat pings |
Emitted when an EventSource request is closed or the client disconnects.
stream.on("close", () => {
// cleanup
});Copyright (c) 2023-2026 Jason Mulligan Licensed under the BSD-3 License
| Back | FazBrowse Home | New Git URL |