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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@ferrow/websocket-client

v2.0.0

Published

A reconnecting WebSocket wrapper with exponential backoff + jitter, heartbeat ping/pong, message queueing while disconnected, and an event emitter API. Bring your own WebSocket implementation.

Readme

websocket-client

CI

A reconnecting WebSocket wrapper for TypeScript/JavaScript. Exponential backoff with jitter, heartbeat ping/pong, a bounded message queue for while you're disconnected, and a small event-emitter API — zero runtime dependencies.

It works with any W3C-compatible WebSocket constructor: the browser's native WebSocket, or Node's ws package. In Node you must bring your own WebSocket implementation — this library does not bundle one, on purpose (see Design notes).

Install

npm install websocket-client
# Node also needs a WebSocket implementation, e.g.:
![CI](https://github.com/FerrowAI/websocket-client/actions/workflows/ci.yml/badge.svg)
npm install ws

Quickstart

Browser:

import { ReconnectingWebSocket } from "websocket-client";

const client = new ReconnectingWebSocket("wss://example.com/socket", {
  WebSocket, // the browser's native global
});

client.on("open", () => console.log("connected"));
client.on("message", (ev) => console.log("received:", ev.data));
client.on("reconnect", () => console.log("reconnected after a drop"));
client.on("close", () => console.log("disconnected"));

client.send("hello"); // queued automatically if not yet open

Node (using ws):

import { ReconnectingWebSocket } from "websocket-client";
import WebSocket from "ws";

const client = new ReconnectingWebSocket("wss://example.com/socket", {
  WebSocket: WebSocket as any,
  heartbeatInterval: 30_000,
});

API

new ReconnectingWebSocket(url: string, options: ReconnectingWebSocketOptions)

| Option | Default | Description | |---|---|---| | WebSocket | required | A WebSocket constructor. | | protocols | undefined | Passed through to the constructor. | | minReconnectDelay | 1000 | Base backoff delay (ms). | | maxReconnectDelay | 30000 | Backoff ceiling (ms). | | reconnectDecay | 2 | Backoff multiplier per attempt. | | jitter | 0.5 | Jitter fraction (0–1) randomized into each delay. | | maxReconnectAttempts | Infinity | Stop retrying after this many attempts. | | heartbeatInterval | 0 (off) | Interval (ms) between app-level pings. | | heartbeatTimeout | 5000 | Force-close if no pong arrives within this window. | | heartbeatMessage | "ping" | Payload sent as the heartbeat. | | heartbeatPongMessage | "pong" | Payload expected back; matching messages are swallowed, not emitted as message. | | maxQueueSize | 100 | Oldest queued messages are dropped past this. | | autoConnect | true | Connect immediately on construction. |

Methods

  • connect() — (re)establish the connection.
  • send(data) — send now if open, otherwise queue (FIFO, bounded by maxQueueSize).
  • close(code?, reason?) — close permanently; disables auto-reconnect.
  • on(event, listener) / off(event, listener)
  • readyState — the underlying socket's readyState, or -1 before first connect.
  • queuedMessageCount — messages currently queued.

Events

open, message, close, reconnect (fires alongside open specifically after a drop), error.

Design notes

The library takes the WebSocket constructor as an injected option instead of depending on ws directly. That keeps it zero runtime dependencies and framework-agnostic: it works unmodified in a browser, and in Node it works with whatever WS client you already have installed (ws, isomorphic-ws, a mock for tests) without version-pinning someone else's package for them. The heartbeat is application-level (send/expect a message) because the WebSocket protocol's native ping/pong frames aren't exposed consistently across environments — if your server already speaks protocol-level ping/pong, you can leave heartbeatInterval at 0 and let the transport handle it.


Sponsored by Ferrow


Part of the ferrow-toolkit collection · Sponsored by Ferrow