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

sseries-of-tubes

v2.0.0

Published

Takes Express/Connect routes and creates stream.Writable endpoints for Server Sent Events

Downloads

4

Readme

sseries-of-tubes

Build Status Coverage Status Dependency Status devDependency Status Downloads

Takes Express/Connect routes and creates stream.Writable endpoints for Server Sent Events.

NPM

Introduction

SSEriesOfTubes attaches to an http.Server and efficiently manages each endpoint for Server Sent Events by creating a writeable StringTube Stream that is piped to every http.Response. StringTube is a utf8 String Stream subclassed from Through2.

Additionally, SSEriesOfTubes can wrap a standard Express/Connect route controller (a function that takes arguments (req, res, next)), poll that controller for data, and push that data to the SSE stream.

This way, any API can easily support REST/polling and Server Sent Events with the same route controller.

SSEriesOfTubes is an EventEmitter based on chrisdickinson/sse-stream, so it handles SSE headers, keep-alive messaging, and emits events around the connection and polling lifecycle.

Installation

npm install --save sseries-of-tubes

Usage

Example

# on server
http           = require "http"
express        = require "express"
SSEriesOfTubes = require "sseries-of-tubes"

server         = new http.Server
app            = express()
sseriesOfTubes = new SSEriesOfTubes server

sendHello = (req, res, next) ->
    res.json hello: "world"

# Sends {"hello": "world"} every 3s to every client connected to /sse/hello
app.get "/sse/hello", sseriesOfTubes.plumb sendHello, 3

...

# on client
source = new EventSource "/sse/hello"
# log {"hello": "world"} as received every 3s
source.onmessage = (data) -> console.log data

Interface

SSEriesOfTubes.StringTube

Pointer to class StringTube, a Through2 Readable/Writable stream.

SSEriesOfTubes::constructor(server, keepAliveInterval)

new SSEriesOfTubes http.Server server, int keepAliveInterval

returns sseriesOfTubes instance

Creates a new SSEriesOfTubes instance that reacts to http.Server "listening" and "close" events.

sseriesOfTubes.plumb(fn, interval)

sseriesOfTubes.plumb Function route, int interval

returns Function route

Creates a new StringTube source which polls a route to write to all clients for an endpoint.

sseriesOfTubes.plumb(fn, interval, event)

sseriesOfTubes.plumb Function route, int interval, String event

returns Function route

Creates a new StringTube source which polls a route to write to all clients for an endpoint, and will send the event event (in EventSource syntax) before sending data.

sseriesOfTubes.plumb()

sseriesOfTubes.plumb()

returns Function route

Creates a new StringTube source which writes to all clients for an endpoint.

sseriesOfTubes.source(url)

sseriesOfTubes.source String url

returns StringTube source

Retrieves the StringTube source for an endpoint.

sseriesOfTubes.combine(router, paths...)

sseriesOfTubes.combine express.Router router, String paths...

returns Function route

Creates a new StringTube source by combining existing StringTube sources referenced by their path. This way different combinations of streams can be provided without duplicating pollers. Requires a router conforming to the express.Router interface for method handle.

sseriesOfTubes.multiplex(router, param = "streams")

sseriesOfTubes.multiplex express.Router router, String param

returns Function route

Creates new StringTube sources by dynamically combining existing StringTube sources, referenced by their path in an comma-separated list provided by the client in the query or body as parameter param. This method is similar to combine, but works with paths that have route parameters. Requires a router conforming to the express.Router interface for method handle.

sseriesOfTubes.destroy()

sseriesOfTubes.destroy()

Ends all SSE client connections and destroys all clients.

Events

connection

sseriesOfTubes.on "connection", (client) ->

On client connection, calls bound function with an instance of Client.

poll

sseriesOfTubes.on "poll", (url) ->

On route start poll, calls bound function with the url endpoint.

plumb

sseriesOfTubes.on "plumb", (url) ->

On route start plumb (creates StringTube source), calls bound function with the url endpoint.

stop

sseriesOfTubes.on "stop", (url) ->

On last client disconnect, calls bound function with the url endpoint.

License

MIT