Module connection

Module connection 

Source
Expand description

WebSocket connection management and message handling.

This module provides the core functionality for managing WebSocket connections, including connection lifecycle, message routing, and broadcasting capabilities.

§Overview

The connection module consists of three main components:

§Architecture

Each WebSocket connection runs two concurrent tasks:

  • Read task: Receives messages from the client
  • Write task: Sends messages to the client via an unbounded channel

This architecture ensures that slow clients don’t block message processing.

§Examples

§Creating and Using a ConnectionManager

use wsforge::prelude::*;
use std::sync::Arc;

let manager = Arc::new(ConnectionManager::new());

// Check connection count
println!("Active connections: {}", manager.count());

// Broadcast a message to all connections
manager.broadcast(Message::text("Hello everyone!"));

§Broadcasting Messages

use wsforge::prelude::*;
use std::sync::Arc;

// Broadcast to all connections
manager.broadcast(Message::text("System announcement"));

// Broadcast to all except one
manager.broadcast_except(&conn_id, Message::text("User joined"));

// Broadcast to specific connections
let target_ids = vec!["conn_1".to_string(), "conn_2".to_string()];
manager.broadcast_to(&target_ids, Message::text("Private message"));

Structs§

Connection
Represents an active WebSocket connection.
ConnectionInfo
Metadata about a WebSocket connection.
ConnectionManager
Manages a collection of active WebSocket connections.

Functions§

handle_websocket
Handles the lifecycle of a WebSocket connection.

Type Aliases§

ConnectionId
A unique identifier for a WebSocket connection.