npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

tiny-eventsource

v3.1.2

Published

Server-Sent Events (SSE) EventEmitter for streaming to HTTP clients

Readme

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.

npm version Build Downloads License: BSD-3

Installation

npm install tiny-eventsource

Quick-Start

import { eventsource } from "tiny-eventsource";

const stream = eventsource({ ms: 2e4 });
stream.init(req, res);
stream.send({ data: "hello" });

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]);
	}
}

API

Constructor

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

eventsource()

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

init(req, res)

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)

send(msg[, event, id])

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)

listenerCount(event)

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

setMaxListeners(n)

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)

stop()

Stops the heartbeat interval if one is active.

stream.stop();

Returns: this (EventSource)

Options

| 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 |

Events

close

Emitted when an EventSource request is closed or the client disconnects.

stream.on("close", () => {
	// cleanup
});

License

Copyright (c) 2023-2026 Jason Mulligan Licensed under the BSD-3 License