JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 29453
  • Score
    100M100P100Q146260F
  • License MIT

Dead simple, dependency-less, spec-compliant server-side events implementation for Node, written in TypeScript.

Package Exports

  • better-sse

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (better-sse) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Better SSE

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

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

See the WHATWG standards section for server-sent events.

See the MDN guide to server-sent events.

Please consider starring the project on GitHub ⭐.

Highlights

  • Compatible with all popular Node HTTP frameworks (http, Express, Koa, Fastify, etc.)
  • Fully written in TypeScript (+ ships with types directly).
  • Thoroughly tested (+ 100% code coverage!).
  • Comprehensively documented with guides and API documentation.
  • Configurable reconnection time.
  • Configurable message serialization and data sanitization (but with good defaults).
  • Trust or ignore the client-given last event ID.
  • Add or override the response status code and headers.
  • Fine-grained control by either sending individual fields of events or sending full events with simple helpers.
  • Pipe streams directly from the server to the client as a stream of events.

Installation

# npm
npm install better-sse

# Yarn
yarn add better-sse

# pnpm
pnpm add better-sse

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

Basic Usage

The following example shows usage with Express, but Better SSE works with any web-server framework (that uses the underlying Node HTTP module).

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

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

app.get("/sse", async (req, res) => {
    const session = await createSession(req, res);
    session.push("Hello world!");
});
// Client
const sse = new EventSource("/sse");

sse.addEventListener("message", (event) => {
    console.log(event.data);
});

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, streams, dispatch controls and more!

Documentation

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

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:

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

Install pnpm:

npm i -g pnpm

Install dependencies:

pnpm i

Run tests:

pnpm t

License

This project is licensed under the MIT license.