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

@rnode-server/websocket-client

v0.5.2

Published

WebSocket client for RNode Server with room support and auto-reconnection

Downloads

5

Readme

@rnode-server/websocket-client

WebSocket client for RNode Server with support for rooms, automatic reconnection, and ping/pong mechanism.

Installation

npm install @rnode-server/websocket-client

Quick Start

import { createWebSocketClient, WebSocketEvent } from '@rnode-server/websocket-client';

// Create client
const client = createWebSocketClient({
  url: 'ws://localhost:3000/chat',
  clientId: 'user123',
  autoReconnect: true,
  onConnect: (event: WebSocketEvent) => {
    console.log('Connected to server!');
  },
  onMessage: (event: WebSocketEvent) => {
    console.log('Received message:', event.data);
  }
});

// Connect
await client.connect();

// Send message
client.send('Hello, world!');

// Join room
client.joinRoom('general');

// Send message to room
client.send('Message to room', 'general');

Key Features

🔌 Automatic Reconnection

  • Exponential backoff between attempts
  • Configurable number of attempts
  • Configurable reconnection interval

🏠 Room System

  • Join rooms
  • Send messages to specific rooms
  • Automatic tracking of current room

🏓 Ping/Pong Mechanism

  • Automatic connection health check
  • Configurable intervals
  • Timeouts for detecting "dead" connections

📱 Events

  • onConnect - connection established
  • onDisconnect - connection closed
  • onMessage - message received
  • onError - error occurred
  • onJoinRoom - joined room
  • onLeaveRoom - left room
  • onPing - ping sent
  • onPong - pong received

API

RNodeWebSocketClient

Constructor

new RNodeWebSocketClient(options: WebSocketOptions)

Methods

connect(): Promise<void>

Connect to WebSocket server.

disconnect(): void

Disconnect from server.

send(data: any, roomId?: string): boolean

Send message. If roomId is not specified, message is sent to current room.

joinRoom(roomId: string): boolean

Join room.

leaveRoom(roomId?: string): boolean

Leave room. If roomId is not specified, leave current room.

isConnected(): boolean

Check connection status.

getState(): number

Get current WebSocket state.

getCurrentRoom(): string | null

Get current room ID.

updateOptions(newOptions: Partial<WebSocketOptions>): void

Update client configuration.

WebSocketOptions

interface WebSocketOptions {
  url: string;                           // WebSocket server URL
  protocols?: string | string[];         // WebSocket protocols
  clientId?: string;                     // Client ID
  autoReconnect?: boolean;                // Automatic reconnection
  reconnectInterval?: number;            // Reconnection interval (ms)
  maxReconnectAttempts?: number;         // Maximum number of attempts
  pingInterval?: number;                 // Ping interval (ms)
  pongTimeout?: number;                  // Pong timeout (ms)
  onConnect?: (event: WebSocketEvent) => void;
  onDisconnect?: (event: WebSocketEvent) => void;
  onMessage?: (event: WebSocketEvent) => void;
  onError?: (event: WebSocketEvent) => void;
  onJoinRoom?: (event: WebSocketEvent) => void;
  onLeaveRoom?: (event: WebSocketEvent) => void;
  onPing?: (event: WebSocketEvent) => void;
  onPong?: (event: WebSocketEvent) => void;
}

WebSocketEvent

interface WebSocketEvent {
  type: 'connect' | 'disconnect' | 'message' | 'error' | 'join_room' | 'leave_room' | 'ping' | 'pong';
  data: any;
  timestamp: number;
}

Usage Examples

Chat Application

import { createWebSocketClient } from '@rnode-server/websocket-client';

const chatClient = createWebSocketClient({
  url: 'ws://localhost:3000/chat',
  clientId: 'user_' + Date.now(),
  autoReconnect: true,
  
  onConnect: () => {
    console.log('Connected to chat');
    chatClient.joinRoom('general');
  },
  
  onMessage: (event) => {
    const message = event.data;
    if (message.type === 'chat') {
      displayMessage(message.data);
    }
  },
  
  onJoinRoom: (event) => {
    console.log(`Joined room: ${event.data.roomId}`);
  }
});

// Connect
await chatClient.connect();

// Send message to chat
function sendMessage(text: string) {
  chatClient.send({
    type: 'chat',
    text,
    timestamp: Date.now()
  }, 'general');
}

Game Application

import { createWebSocketClient } from '@rnode-server/websocket-client';

const gameClient = createWebSocketClient({
  url: 'ws://localhost:3000/game',
  clientId: 'player_' + Math.random().toString(36).substr(2, 9),
  autoReconnect: true,
  pingInterval: 1000, // Frequent pings for games
  
  onConnect: () => {
    console.log('Connected to game server');
  },
  
  onMessage: (event) => {
    const gameEvent = event.data;
    switch (gameEvent.type) {
      case 'player_move':
        updatePlayerPosition(gameEvent.playerId, gameEvent.position);
        break;
      case 'game_state':
        updateGameState(gameEvent.state);
        break;
    }
  }
});

// Connect to game room
await gameClient.connect();
gameClient.joinRoom('game_room_1');

// Send player movement
function sendPlayerMove(position: { x: number, y: number }) {
  gameClient.send({
    type: 'player_move',
    position,
    timestamp: Date.now()
  }, 'game_room_1');
}

Build

npm run build

Development

npm run dev

License

MIT