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

v0.1.0

Published

Server-Sent Events utility written in TypeScript

Downloads

43

Readme

sse-channels

Server-Sent Events utility written in TypeScript.

Features

  • Simple
  • Built in types
  • Framework agnostic
  • Flexible, independent connections make it easy create your custom channel logic
  • History is maintained automatically
  • Channels automatically sends missed events upon reconnections
  • Temporary Channels (if you need a channel per active user for example)

Usage

Connection

It's the building block, each connection is independent with its own ping and timeout. Connections don't hold message history or any special state.

const express = require("express");
const { Connection } = require("sse-channels");

const app = express();

app.get("/sse", function (req, res) {
  const conn = new Connection(req, res, {
    ping: true, // automatically sends an empty comment (":\n") each ping interval
    pingInterval: 50 * 1000, // 50 seconds, this is the default interval
    timeout: 12 * 60 * 60 * 1000, // 12 hours max, to avoid any leak
  });
  // optionally sends a connection confirmation
  conn.send({ comment: "OK" });

  conn.send({
    event: "my-event",
    data: JSON.stringify({}),
    id: "id",
  });
});

Channels

To broadcasting an event.

const { Connection, Channel } = require("sse-channels");

// create a channel
const weatherChannel = new Channel({
  historySize: 500 // default value
});

app.get("/sse/weather", function (req, res) {
  const conn = new Connection(req, res, {
    ping: true,
    timeout: 12 * 60 * 60 * 1000,
  });
  conn.send({ comment: "OK" });

  // add new connection to the weather channel
  // if it's a reconnection (the connection has a lastEventID property) channel automatically sends newer messages from its history
  weatherChannel.add(conn);

  // the connection is automatically removed from the channel when a close event is emitted
  // the connection timeout by default automatically ends the connection and also emit a close event
});

// somewhere else, broadcast event to all channel connections
weatherChannel.send({
  event: "weather-event",
  data: JSON.stringify({}),
  id: "id",
});

Note: currently, the history only works properly if a connection is attached to only 1 channel, missed messages upon reconnection won't work as expected if the same connection is added to multiple channels.

Temporary Channels

For workloads with transient group of connections.

Like to have a channel per active user:

const { Connection, Channel, MapListener } = require("sse-channels");

// create a channel store, automatically removes channel when receive a close event
const channelStore = new MapListener();

app.get("/sse/user/:userID", function (req, res) {
  const { userID } = req.params;

  const conn = new Connection(req, res, {
    ping: true,
    timeout: 12 * 60 * 60 * 1000,
  });
  conn.send({ comment: "OK" });

  // check if user already have an active channel
  const userChannel = channelStore.get(userID);
  if (userChannel) {
    userChannel.add(conn);
  } else {
    const userChannel = new Channel({
      emptyTimeout: 10 * 60 * 1000, // after 10 minutes while empty channel will emit a close event
    });
    userChannel.add(conn);

    // add userChannel to our channel store
    channelStore.set(userID, userChannel);
  }
});

// somewhere else, broadcast event to specific user connections
const userChannel = channelStore.get(userID);
userChannel.send({
  event: "user-event",
  data: JSON.stringify({}),
  id: "id",
});

IMPORTANT

While a major version 1.0.0 is not released if you want to use this package in production please lock by its minor version like this >=0.1.0 <0.2.0. The public API SHOULD NOT be considered stable, but I'll not break it with PATCH versions.