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 🙏

© 2024 – Pkg Stats / Ryan Hefner

sse-gen

v1.0.0

Published

Server-Sent Event client using Generators

Downloads

9

Readme

sse-gen

version issues downloads license

A Server-Sent Event (SSE) client using Generators.

Installation

This package is distributed via npm:

npm install sse-gen

Methods

Instantiating a client

To instantiate a client, call new on the class, and pass it a url value.

const client = new SSEClient<T>("/events");

options

An optional options object can be passed as a second argument.

type SSEClientOptions = {
  onStatusUpdate?: StatusUpdateHandler;
  reconnect?: { maxAttempts: number; delay: number };
};

The use of those options is explained further down this document.

Connecting to the server

To connect to the server (based on the URL passed to the constructor), call the connect method on the instantiated client.

client.connect();

Closing the connectino to the server

To close the connect to the server, call the close method on the instantiated client.

client.close();

Handle SSE messages

To add an handled for messages sent by the server, pass an EventHandlerMessage function to the on method.

client.on((event) => {
  console.log(event.data);
});

Note that if you are receiving a JSON string, you might need to parse it using JSON.parse().

Asynchronous handler

The main motivation of using a Generator to handle events in this SSE client is the possibility to have asynchronous message handlers. Using a Generator ensures that messages will be processed one by one, and that new messages will wait for the previous messages to be processed.

Error handling

Automatic reconnection

If passed a reconnect option to the constructor, the client will try to reconnect automatically.

The number of times the client will try to reconnect is defined by the maxAttempts property of the reconnect option. The delay between each reconnection attempt is defined by the delay property of the reconnect option.

const client = new SSEClient("/events", {
  reconnect: {
    maxAttempts: 3,
    delay: 1000,
  },
});

Custom error handling

To add further custom error handling, pass an EventHandlerError function to the catch method.

client.catch((event) => {
  console.error(event);
});

Note that the client calls close when receiving an error.

Getters

url

To retrieve the URL the client is connected to, use the url getter on the instantiated client.

const client = new SSEClient<T>("/events");
client.url; // "/events"

status

The client can be in different statuses:

enum SSEClientStatus {
  CONNECTING = "CONNECTING",
  OPEN = "OPEN",
  CLOSED = "CLOSED",
}

To retrieve the status of the client, use the status getter on the instantiated client.

const client = new SSEClient<T>("/events");
client.connect();
client.status; // "CONNECTING"
// ...
client.status; // "OPEN"
// ...
client.close();
client.status; // "CLOSED"

Custom status update handler

You can also pass a custom handler for status updates in the constructor.

const client = new SSEClient("/events", {
  onStatusUpdate: (status) => {
    console.log(status);
  },
});