Skip to content

WsConnections

This file defines the WsConnections class, which manages all active WebSocket connections in the @sodacore/core WebSocket (WS) module. It provides methods to add, remove, retrieve, broadcast to, and manage multiple WebSocket connections, enabling efficient real-time communication with connected clients.


Features

  • Connection Management: Add, remove, retrieve, and check for active WebSocket connections by ID.
  • Broadcasting: Send commands or raw messages to all, some, or all except specified connections.
  • Bulk Operations: Clear all connections and close them gracefully.
  • Dependency Injection: Uses the @Provide decorator for integration with the Sodacore DI system.

Usage

typescript
import WsConnections from './provider/ws-connections';

// Add a new connection
wsConnections.addConnection(id, wsContext);

// Broadcast a command to all clients
wsConnections.broadcast('update', { message: 'Hello, all!' });

// Send a raw message to specific clients
wsConnections.broadcastRawFor(['id1', 'id2'], 'Ping');

// Remove a connection
wsConnections.removeConnection(id);

API

Connection Management

  • addConnection(id: string, context: WsContext): void Adds a new WebSocket connection by ID.

  • removeConnection(id: string): void Removes a WebSocket connection by ID.

  • getConnection(id: string): WsContext | undefined Retrieves a connection by ID.

  • hasConnection(id: string): boolean Checks if a connection exists by ID.

  • getConnections(): Map<string, WsContext> Returns the map of all active connections.

  • getConnectionCount(): number Returns the number of active connections.

  • clearConnections(reason?: string): void Closes and removes all connections, optionally providing a reason.


Broadcasting

  • broadcast(command: string, context: Record<string, any> = {}): void Sends a command and context to all connections.

  • broadcastRaw(data: string): void Sends a raw string message to all connections.

  • broadcastFor(id: string | string[], command: string, context: Record<string, any> = {}): void Sends a command and context to specific connection(s) by ID.

  • broadcastRawFor(id: string | string[], data: string): void Sends a raw string message to specific connection(s) by ID.

  • broadcastExcept(id: string | string[], command: string, context: Record<string, any> = {}): void Sends a command and context to all connections except those specified by ID.

  • broadcastRawExcept(id: string | string[], data: string): void Sends a raw string message to all connections except those specified by ID.


How It Works

  • Maintains a Map of connection IDs to WsContext instances.
  • Provides methods to add, remove, and retrieve connections.
  • Broadcast methods iterate over the map and send messages or commands as needed.
  • clearConnections ensures all connections are closed before clearing the map.

Notes

  • Designed for internal use by the Sodacore WebSocket system.
  • All broadcast methods support both single and multiple connection IDs.
  • Ensures efficient and flexible management of real-time WebSocket connections.

Released under the Apache-2.0 License.