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 🙏

© 2025 – Pkg Stats / Ryan Hefner

rxwebsocket

v1.0.7

Published

RxJS WebSocket Advanced Wrapper

Readme

Rx WebSocket Advanced Wrapper

This TypeScript module is intended to be a drop-in replacement for the standard RxJS WebSocketSubject, but with some additional capabilities:

  1. Separate types for input and output data. In fact it is very rare if not impossible that the same data type is being used for input and output for a WebSocket as it is provided by standard RxJS solution, so this problem is resolved by the module.
  2. Reconnection with exponential back-off, which is completely absent in standard solution.
  3. Buffering outgoing messages during reconnect with auto-sending when connection is re-established. This includes 'subscribe' messages for multiplex.
  4. Ability to keep the connection alive when the last multiplex consumer is off. Standard behavior is to disconnect, which is not desirable in some cases.
  5. Special property state$ which is a Subject to track the connection state, emitting values DISCONNECTED, CONNECTING, CONNECTED.
  6. Easy logging of all actions and messages with debug configuration boolean parameter.

Install

npm i rxwebsocket

The module has only one dependency which is rxjs itself.

It contains both ESM modern version and CommonJS version for NodeJS legacy.

Usage

Configuration URL or object:

import { RxWebSocketSubjectConfig } from 'rxwebsocket';
const config: RxWebSocketSubjectConfig = {
    url: 'wss://example.com/websocket',
    debug: true,
};

// simplified config can also be just a URL string:
const config = 'wss://example.com/websocket';

Instance via function:

import { rxWebSocket } from 'rxwebsocket';
const socket$ = rxWebSocket<Input, Output>(config);

Instance via class:

import { RxWebSocketSubject } from 'rxwebsocket';
const socket$ = new RxWebSocketSubject<Input, Output>(config);

Tracking connection state

socket$.state$.subscribe({
  next: (state) => {
    console.log('Connection state:', state);
  },
});

Sending data

Syntax is the same as with standard RxJS WebSocketSubject.

socket$.next({ some: 'data' });

Receiving data

Syntax is the same as with standard RxJS WebSocketSubject.

socket$.subscribe({
  next: (message) => {
    console.log('Incoming message:', message);
  },
});

Multiplexing

Syntax is the same as with standard RxJS WebSocketSubject.

Please note that in case of reconnect 'subscribe' message will be sent automatically, so you don't have to implement that manually.

If you don't want connection to be closed after the last multiplex consumer unsubscribes, specify keepAlive: true in config object.

socket$.multiplex(
  () => ({ type: 'subscribe', symbol: 'AAPL' }),
  () => ({ type: 'unsubscribe', symbol: 'AAPL' }),
  (data) => data.type === 'trade',
);

Configuration parameters

RxWebSocketSubjectConfig extends standard WebSocketSubjectConfig with extra optional parameters:

maxAttempts: number

Number of reconnection attempts to make. Defaults to Infinity.

maxDelay: number

Number of milliseconds that limits exponential delay to reconnect. Defaults to 30000.

delay: number

Number of milliseconds to start exponential delay with. Defaults to 1000.

keepAlive: boolean

Do not disconnect when the last multiplex consumer unsubscribes. Defaults to false.

debug: boolean

Enable logging of all actions and messages (with console.debug). Defaults to false.