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

@takadenoshi/express-sse

v0.6.3

Published

An Express middleware for Server-Sent Events (EventSource)

Downloads

26

Readme

express-sse

Fork Differences

This project was forked from dpskvn/express-sse and introduces the following changes:

Heartbeat/ping event

Adds support for a ping event that can be used to keep idle connections alive through some networking middleware like haproxy.

Specify options.pingInterval with the desired heartbeat interval in ms.

The heartbeat event name will default to ping unless overridden with options.pingEvent.

new SSE([], { pingInterval: 20_000, pingEvent: 'thump' })

This approach can be utilized from the frontend to also detect stale connections (expect a ping event every N seconds.)

An alternative approach to this would be to emit a comment, which is not currently supported by this library.

Change initial event behavior

The original initial event implementation had the following unwanted behavior: When a new client connected, the initial event would be re-broadcast to all connected clients.

This has been changed to the expected approach of broadcasting the initial event only to new clients.

Make initial event optional

The initial event defaulted to [] when not defined. This implementation changes the default to be undefined and not being broadcast unless explicitly specified.

Retry support

SSE supports a retry field which instructs the client to change the default reconnection timeout.

Set options.retry to a value in milliseconds and the first event emitted will include a retry header field.

new SSE([], { retry: 5_000 });

Omit IDs option

The constructor options support .noID to skip assigning IDs to events that do not explicitly have one.

An Express middleware for quick'n'easy server-sent events.

Support for express without compression

The res.flush call is made optional, which enabled support for express without the compression middleware.

Error handling while setting headers

The header setting parts that can throw have been wrapped in a try/catch


The original README follows with the installation links replaced.


About

express-sse is meant to keep things simple. You need to send server-sent events without too many complications and fallbacks? This is the library to do so.

Installation:

npm install --save @takadenoshi/express-sse

or

yarn add @takadenoshi/express-sse

Usage example:

Options:

You can pass an optional options object to the constructor. Currently it only supports changing the way initial data is treated. If you set isSerialized to false, the initial data is sent as a single event. The default value is true.

var sse = new SSE(["array", "containing", "initial", "content", "(optional)"], { isSerialized: false, initialEvent: 'optional initial event name' });

Server:

var SSE = require('@takadenoshi/express-sse');
var sse = new SSE(["array", "containing", "initial", "content", "(optional)"]);

...

app.get('/stream', sse.init);

...

sse.send(content);
sse.send(content, eventName);
sse.send(content, eventName, customID);
sse.updateInit(["array", "containing", "new", "content"]);
sse.serialize(["array", "to", "be", "sent", "as", "serialized", "events"]);

Client:

var es = new EventSource('/stream');

es.onmessage = function (event) {
  ...
};

es.addEventListener(eventName, function (event) {
  ...
});