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

persistent-websocket

v1.0.2

Published

An automatically-reconnecting websocket wrapper that respects server reachability and good backoff practices

Downloads

288

Readme

Build Status npm version

Persistent Websocket

An automatically-reconnecting websocket wrapper that respects server reachability and good backoff practices

Features

  • Optionally ping the backend (using a custom function) to make sure you're not on a zombie connection
  • Optionally check for basic internet connectivity before trying to reconnect (via a configured url endpoint)
  • "Decorrelated jitter" backoff
  • umd / universal library
  • Configurable timeouts, ping intervals, and backoff delay limits

Usage

import {PersistentWebsocket} from "persistent-websocket";

const pws = new PersistentWebsocket("ws://mysite.com/", {
  pingSendFunction: (pws) => pws.send("ping"),
  reachabilityTestUrl: "/favicon.ico"  
});
pws.onmessage = (e) => console.log(e);
pws.onopen = (e) => console.log(e);
pws.onclose = (e) => console.log(e);
pws.onerror = (e) => console.log(e);

pws.open(); // Must be explicitly opened, unlike regular WebSocket

// Now you can use the PersistentWebsocket instance like a plain WebSocket instance, 
// except this instance will automatically try to reconnect if the connection dies 

Options

The full list of available options is:

  • debug boolean (default false):
    Controls logging of verbose/debug output
  • initialBackoffDelayMillis numeric (default 500):
    Delay before first reconnection attempt (also acts as a minimum delay)
  • maxBackoffDelayMillis numeric (default 120000):
    Maximum delay between reconnection attempts
  • pingSendFunction function (no default):
    An optional function that takes the PersistentWebsocket instance as its only parameter.
    When left undefined, pings will not be performed.
  • pingIntervalSeconds numeric (default 30):
    If the remote end of the websocket connection hasn't sent anything after this number of seconds, a ping will be sent (via pingSendFunction) to probe the connection.
    Note that pings aren't sent at regular intervals. They're only sent when the line is otherwise quiet.
  • pingTimeoutMillis numeric (default 2000):
    How long to wait for a ping response before calling the connection dead
  • connectTimeoutMillis numeric (default 3000):
    How long to wait for the websocket connection to reach a WebSocket.OPEN ready state
  • reachabilityTestUrl string (no default):
    An optional url to use to test for internet connectivity (may be absolute or relative)
    A HEAD request will be sent to this url before trying to reconnect, and if it fails, the library will reset the backoff timing to its initial value and poll that url until it successfully responds.
    If left undefined, no reachability/internet connectivity check will be performed.
  • reachabilityTestTimeoutMillis numeric (default 2000):
    How long to wait for a response from the reachabilityTestUrl
  • reachabilityPollingIntervalMillis numeric (default 3000):
    How long to between a failed reachability test and the next request to the reachabilityTestUrl

Events

In addition to the standard websocket events, the library also provides a "beforereconnect" event. This event is fired when a reconnection is scheduled (as opposed to attempted), and has the following properties:

  • attemptNumber numeric
    The number of this attempt relative to the last disconnect
  • waitMillis numeric
    The timeout until the next connection attempt will be made (in milliseconds)

It also adds a couple fields to existing websocket events:

  • wasExpected boolean
    Added to the close event
  • wasReconnect boolean
    Added to the open event

License

MIT