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

typed-sse

v0.1.14

Published

Typed helpers for Server-Sent Events (EventSource) with JSON parsing and optional reconnect.

Downloads

34

Readme

typed-sse

npm npm dependencies npm downloads

GitHub GitHub forks GitHub issues CI

status license

⚠️ Warning:
The naming convention for this library is not yet finalized and may change between versions. Do not use it in production environments until the first LTS release!

🚧 The beta version is under active development.

typed-sse is a TypeScript library that provides a robust, type-safe wrapper for the native EventSource API. It enables you to handle server-sent events (SSE) with strict type checking, ensuring that event payloads match your expected types. This helps catch errors at compile time and makes your SSE code safer, more maintainable, and easier to refactor.

Features

  • Type-safe event handling: Define your event map interface and get compile-time safety for all event payloads.
  • Flexible event mapping: Supports custom event types and payloads, including nested objects and arrays.
  • Automatic JSON parsing: Event data is parsed and validated before reaching your handler.
  • Retry logic: TypedEventSource supports configurable automatic reconnection with exponential backoff.
  • Works in browser and Node.js: Use the native EventSource or provide a polyfill for Node environments.

Two Ways to Use

You can use typed-sse in two ways:

  1. With the classic EventSource API for minimal changes to your existing code, using addTypedDataEventListener for type-safe listeners.
  2. With the custom TypedEventSource class for a streamlined, type-safe experience and advanced features.

Method 1: Classic EventSource (with type-safe listeners)

Use the standard EventSource and add type-safe event listeners with addTypedDataEventListener. This lets you keep your existing code style while gaining type safety for event payloads.

import { addTypedDataEventListener } from "typed-sse";

interface ConnectedData {
  id: number;
  text: string;
}

const eventSource = new EventSource("/api/stream");

// using a simple data structure as type
const connectedListener = addTypedDataEventListener<ConnectedData>(
  eventSource,
  "connected",
  (data) => {
    console.log("Connection text:", data.text);
  }
);

// To stop listening:
connectedListener.stopListening();
eventSource.close();

Method 2: TypedEventSource (recommended)

Use the TypedEventSource class for a fully type-safe, ergonomic SSE experience. It supports automatic JSON parsing, retry logic, and easy listener management.

import { TypedEventSource } from "typed-sse";

interface CustomData {
  text: string;
  foo: { title: string; description: string; id: number };
  bar: { list: number[]; id: number; name: string };
}

interface MyEvents {
  connected: { id: string };
  message: { text: string };
  custom: CustomData;
}

//simple :
const typedEventSource = new TypedEventSource<MyEvents>("/path/to/stream"});


const connectedListener = typedEventSource.on("connected", (data) => {
  console.log("Connection id:", data.id);
});

const customListener = typedEventSource.on("custom", (data) => {
  console.log("Custom bar list:", data.bar.list);
});

// To stop listening:
connectedListener.stopListening();
customListener.stopListening();
typedEventSource.close();



//or with options... :
/*

const sse = new TypedEventSource<MyEvents>("/path/to/stream", {
  withCredentials: true,
  retry: { base: 1000, max: 30000 },
});

*/

API Reference

TypedEventSource

  • constructor(url: string, opts?: CreateOptions, ES?: EventSourceConstructor)
  • .on(eventName, handler) — Add a type-safe listener for an event.
  • .close() — Close the connection and stop all listeners.
  • .current — Get the current EventSource instance (or null).

addTypedDataEventListener

  • addTypedDataEventListener<T>(eventSource, eventName, handler, errorHandler?) — Add a type-safe listener to an existing EventSource.

👨‍💻 Author

MrRise@RisingCorporation
Made with ❤️ and Bun 🐇