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

react-ws-manager

v0.1.1

Published

Type-safe WebSocket manager for real-time applications. Framework-agnostic core with first-class React support. Supports multi-endpoint connections, auto-reconnect, and subscription-based messaging.

Downloads

13

Readme

react-ws-manager

npm | license

🇺🇸 English | 🇰🇷 Korean

⚡ A framework-agnostic WebSocket manager for real-time apps with first-class React support.


🧠 Why not just use native WebSocket?

Because this is what happens in real apps:

  • ❌ multiple connections → duplicated logic
  • ❌ reconnect handling → inconsistent
  • ❌ state sync → manual
  • ❌ scaling → painful

This library solves all of that.


🏭 Built for real-world systems

This library was originally built for:

  • multi-service architectures (MSA)
  • event-driven backend systems
  • real-time monitoring dashboards

Where:

  • each service exposes its own WebSocket endpoint
  • UI needs to subscribe to multiple streams
  • state must stay consistent across features

✨ Why this exists

❗ Problem

Most WebSocket implementations in React apps look like this:

  • scattered useEffect hooks
  • duplicated reconnect logic
  • hard-to-track connection state
  • messy event handling
  • impossible to scale across multiple endpoints

💡 Solution

This library provides a centralized, type-safe WebSocket layer:

  • manage multiple WebSocket connections
  • subscribe to messages declaratively
  • auto reconnect with exponential backoff
  • built-in heartbeat support
  • queue messages before connection opens
  • fully typed message handling
  • React integration out of the box

🎯 When should you use this?

This library is designed for applications that:

  • use multiple WebSocket endpoints
  • consume real-time events from different services
  • need structured subscription-based messaging
  • want to avoid scattered WebSocket logic

Real-world examples

  • 🏭 Manufacturing / IoT monitoring systems
  • 📊 Real-time dashboards (logs, metrics, alerts)
  • 💬 Multi-channel chat applications
  • 🚨 Event-driven notification systems

⚡ Features

  • 🔌 Multi-endpoint WebSocket management
  • 🔁 Auto reconnect (exponential backoff + retry control)
  • ❤️ Heartbeat support for connection stability
  • 📬 Message queue before connection is ready
  • 📡 Subscription-based event system
  • 🧠 Fully type-safe (TypeScript-first)
  • ⚛️ React hooks (useWebSocket)
  • 🌲 Tree-shakable design (core + react split)
  • 🚀 SSR-safe (Next.js compatible)

🔥 Without vs With

❌ Without this library

useEffect(() => {
  const ws = new WebSocket(url);

  ws.onmessage = (e) => { ... };
  ws.onclose = () => reconnect();

  return () => ws.close();
}, []);
  • manual reconnect logic
  • duplicated code
  • hard to scale
  • no structure

✅ With react-ws-manager

const { data, status } = useWebSocket('event');
  • declarative subscriptions
  • centralized connection management
  • built-in reconnect & heartbeat
  • type-safe message flow

📦 Installation

npm install react-ws-manager

🚀 Quick Start (with React)

import { createWebSocketManager } from 'react-ws-manager';
import { WebSocketProvider, useWebSocket } from 'react-ws-manager/react';

type WS = {
  chat: { text: string };
};

const ws = createWebSocketManager<WS>({
  baseUrl: 'ws://localhost:8080',
  endpoints: { chat: '' },
});

function App() {
  return (
    <WebSocketProvider manager={ws}>
      <Chat />
    </WebSocketProvider>
  );
}

function Chat() {
  const { data, status } = useWebSocket<WS, 'chat'>('chat');

  return (
    <div>
      <p>Status: {status}</p>
      <pre>{JSON.stringify(data)}</pre>
    </div>
  );
}

👉 No useEffect. No manual reconnect logic.

🧠 TypeScript Support

type WS = {
  event: { id: number; message: string };
  chat: { user: string; text: string };
};

const ws = createWebSocketManager<WS>({
  baseUrl: 'ws://localhost:8080',
  endpoints: {
    event: '/event',
    chat: '/chat',
  },
});

👉 Fully typed messaging out of the box.


⚙️ Advanced Configuration

const ws = createWebSocketManager({
  baseUrl: 'ws://localhost:8080',
  endpoints: { event: '/event' },

  parser: (e) => JSON.parse(e.data),
  serializer: (data) => JSON.stringify(data),

  retry: {
    attempts: 5,
    delay: (count) =>
      Math.min(1000 * 2 ** count, 30000),
  },

  heartbeat: {
    interval: 10000,
    message: () => 'ping',
  },

  debug: true,
  autoConnect: true
});

📁 Import API

// Core
import { createWebSocketManager } from 'react-ws-manager';

// React
import { useWebSocket, WebSocketProvider } from 'react-ws-manager/react';

🧩 API

createWebSocketManager(options)

| Option | Description | | ------------ | ---------------------- | | baseUrl | WebSocket base URL | | endpoints | Endpoint map | | parser | Custom message parser | | serializer | Custom send serializer | | retry | Reconnect strategy | | heartbeat | Heartbeat config | | debug | Enable logs | | autoConnect| Enable Autoconnect |


Methods

ws.connect(key)
ws.close(key)
ws.subscribe(key, handler)
ws.subscribeStatus(key, handler)
ws.send(key, message)
ws.getStatus(key)

🏗 Architecture

React App
   ↓
useWebSocket
   ↓
WebSocketProvider
   ↓
WebSocket Manager
   ↓
Multiple WebSocket Connections
   ↓
Backend Services

💡 Design Philosophy

  • WebSocket should feel like state, not side-effects
  • Core is framework-agnostic
  • React is just a thin adapter
  • Subscription over polling
  • Built for real-world systems (not toy examples)

📌 Notes

  • React is a peer dependency (>=18)
  • Works with Next.js, Vite, CRA
  • SSR-safe
  • No unnecessary re-renders

📄 License

license


⭐ Support

If this project helps you, please consider giving it a star ⭐

It really helps the project grow.