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

v1.0.0

Published

SSE as a ReadableStream

Downloads

5

Readme

SSE Feed

Server-Sent Events consumer supporting backpressure.

Introduction

EventSource API to consume SSE feed is not available in Node.js. There are plenty of polyfills for EventSource. There are some consumers of SSE connection. All of them suffer in some way:

  • not handling automatic reconnects,
  • or having a weird buffering behaviour,
  • or relying on Node-specific http API,
  • or not supporting backpressure.

This library tries to solve that. It does reconnect as EventSource does, it does not buffer input, it supports backpressure, it connects through a native fetch.

Installation

As usual, do

npm add sse-feed

Usage

First, create an instance of SSEFeed, then consume the stream as sseFeed.stream(). It presents a JS-native ReadableStream.

import { SSEFeed } from 'sse-feed'
const sseFeed = new SSEFeed('http://example.com/sse')
// Use iteration
for await (const event of sseFeed.stream()) {
  // Do something with the event
  console.log(event)
}
// ...or use more native streaming
sseFeed.stream().pipeTo(...)

Cancellation

When you are done with consuming the events, call sseFeed.close(). It tells the stream to gracefully stop.

"open" and "close" events

To keep track of the feed connectivity, you could subscribe to open and error events. SSEFeed implements EventTarget interface. These events are independent of SSE stream events.

import { SSEFeed } from 'sse-feed'
const sseFeed = new SSEFeed('http://example.com/sse')
sseFeed.stream().pipeTo(...)
sseFeed.addEventListener('open', openEvent => {
console.log('Opened')
})

open and error events could be emitted multiple times during the streaming as per the Server-Sent Events specification.

One SSEFeed instance - One stream

Calling sseFeed.stream() initiates a HTTP connection. The connection lifecycle is tracked by open and error events. Hence, there is a one-to-one correspondence between ReadableStream of sseFeed.stream() and an instance of SSEFeed. Hence, you can call sseFeed.stream() once per sseFeed instance.

If you need multiple consumers, you could use tee() method, like sseFeed.steram().tee(), or you could create another SSEFeed instance.

Custom fetch properties

SSEFeed constructor accepts an optional second parameter. It can be { withCredentials: true } like for EventSource. Or it can be fetch parameters like { method: 'POST' }.

Contributing

Please, feel free to raise issues, ask questions, make PRs and so on.

License

MIT or Apache-2.0