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 🙏

© 2025 – Pkg Stats / Ryan Hefner

better-sse

v0.15.1

Published

Dead simple, dependency-less, spec-compliant server-sent events implementation written in TypeScript.

Readme

Better SSE

A dead simple, dependency-less, spec-compliant server-sent events implementation written in TypeScript.

This package aims to be the easiest to use, most compliant and most streamlined solution to server-sent events that is framework-agnostic and feature-rich.

Please consider starring the project on GitHub ⭐.

Why use Server-sent Events?

Server-sent events (SSE) is a standardised protocol that allows web-servers to push data to clients without the need for alternative mechanisms such as pinging, long-polling or WebSockets.

Using SSE can allow for significant savings in bandwidth and battery life on portable devices and will work with your existing infrastructure as it operates directly over the HTTP protocol without the need for the connection upgrade that WebSockets require.

Compared to WebSockets it has comparable performance and bandwidth usage, especially over HTTP/2, and natively includes event ID generation and automatic reconnection when clients are disconnected.

Read the Getting Started guide for more.

Highlights

  • Compatible with all popular HTTP frameworks (Express, Hono, Fastify, Nest, Next.js, Bun, Deno, etc.)
  • Fully written in TypeScript (+ ships with types directly).
  • Thoroughly tested (+ 100% code coverage!).
  • Comprehensively documented with guides and API documentation.
  • Channels allow you to broadcast events to many clients at once.
  • Event buffers allow you to batch events for increased performance and lower bandwidth usage.
  • Configurable reconnection time, message serialization and data sanitization (with good defaults).
  • Trust or ignore the client-given last event ID.
  • Automatically send keep-alive pings to keep connections open.
  • Add or override the response status code and headers.
  • Pipe streams and iterables directly from the server to the client as a series of events.
  • Support for popular EventSource polyfills event-source-polyfill and eventsource-polyfill.

See a comparison with other SSE libraries in the documentation.

Installation

Better SSE is published as a package on npm and the JSR. You can install it with any package manager:

npm install better-sse
yarn add better-sse
pnpm add better-sse
bun add better-sse
deno install jsr:@mwid/better-sse

Better SSE ships with types built in. No need to install from DefinitelyTyped for TypeScript users!

Usage

The examples below show usage with Express and Hono, but Better SSE works with any web-server framework that uses the Node HTTP module or the Fetch API.

See the Recipes section of the documentation for use with other frameworks and libraries.


Use sessions to push events to clients:

// Server - Express
import { createSession } from "better-sse"

app.get("/sse", async (req, res) => {
	const session = await createSession(req, res)
	session.push("Hello world!", "message")
})
// Server - Hono
import { createResponse } from "better-sse"

app.get("/sse", (c) =>
    createResponse(c.req.raw, (session) => {
        session.push("Hello world!", "message")
    })
)
// Client
const eventSource = new EventSource("/sse")

eventSource.addEventListener("message", ({ data })) => {
	const contents = JSON.parse(data)
	console.log(contents) // Hello world!
})

Use channels to send events to many clients at once:

import { createSession, createChannel } from "better-sse"

const channel = createChannel()

app.get("/sse", async (req, res) => {
	const session = await createSession(req, res)

	channel.register(session)

	channel.broadcast("A user has joined.", "join-notification")
})

Use batching to send multiple events at once for improved performance and lower bandwidth usage:

await session.batch(async (buffer) => {
    await buffer.iterate(["My", "huge", "event", "list"])
})

Loop over sync and async iterables and send each value as an event:

const iterable = [1, 2, 3]

await session.iterate(iterable)

Pipe readable stream data to the client as a stream of events:

const stream = Readable.from([1, 2, 3])

await session.stream(stream)

Check the API documentation and live examples for information on getting more fine-tuned control over your data such as managing event IDs, data serialization, event filtering, dispatch controls and more!

Documentation

API documentation, getting started guides and usage with other frameworks is available on the documentation website.

Contributing

This library is always open to contributions whether it be code, bug reports, documentation or anything else.

Please submit suggestions, bugs and issues to the GitHub issues page.

For code or documentation changes submit a pull request on GitHub.

Local Development

Install Node (with n):

curl -L https://git.io/n-install | bash
n auto

Install dependencies (with pnpm):

npm i -g pnpm
pnpm i

Run tests (with Vitest):

pnpm t

Lint and format (with Biome):

pnpm lint
pnpm format

Bundle for distribution (with tsup):

pnpm build

License

This project is licensed under the MIT license.