Crate stream_tungstenite

Crate stream_tungstenite 

Source
Expand description

stream-tungstenite: WebSocket client with automatic reconnection.

This library provides a robust WebSocket client that handles:

  • Automatic reconnection with configurable retry strategies
  • Application-level handshakes (authentication, subscriptions)
  • Extension system for lifecycle and message handling
  • Connection state management and monitoring

§Quick Start

use stream_tungstenite::{WebSocketClient, ClientConfig};
use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create client with builder pattern
    let client = WebSocketClient::builder("wss://example.com/ws")
        .receive_timeout(std::time::Duration::from_secs(30))
        .build();

    // Subscribe to messages before running
    let mut messages = client.subscribe();

    // Run client in background
    let client = Arc::new(client);
    tokio::spawn({
        let client = client.clone();
        async move { client.run().await }
    });

    // Receive messages
    while let Ok(msg) = messages.recv().await {
        println!("Received: {:?}", msg);
    }

    Ok(())
}

§Architecture

┌─────────────────────────────────────────────────────────────────┐
│                      WebSocketClient                            │
│  - High-level API for WebSocket connections                     │
│  - Automatic reconnection with retry strategies                 │
│  - Message broadcasting via channels                            │
└─────────────────────────────────────────────────────────────────┘
                             │
         ┌───────────────────┼───────────────────┐
         ▼                   ▼                   ▼
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│   Handshake     │ │   Extension     │ │   Connection    │
│   - Auth        │ │   - Lifecycle   │ │   - State       │
│   - Subscribe   │ │   - Messages    │ │   - Retry       │
│   - Chained     │ │   - Logging     │ │   - Supervisor  │
└─────────────────┘ └─────────────────┘ └─────────────────┘

Re-exports§

pub use client::ClientConfig;
pub use client::Sender;
pub use client::WebSocketClient;
pub use client::WebSocketClientBuilder;
pub use connection::ConnectionEvent;
pub use error::ClientError;
pub use error::ConnectError;
pub use error::DisconnectReason;
pub use error::HandshakeError;
pub use error::ReceiveError;
pub use error::SendError;
pub use message::SharedMessage;

Modules§

client
WebSocket client with automatic reconnection.
connection
Connection management module.
context
Unified connection context used by handshake and extension code.
error
Unified error types for the WebSocket client library.
extension
Extension system for WebSocket client.
handshake
Handshake module for application-level WebSocket handshakes.
message
Message handling module.
prelude
Prelude for convenient imports
tokio_tungstenite
Re-export tokio-tungstenite types
transport
Transport layer for establishing underlying connections.

Macros§

chain_handshakers
Convenience macro for creating chained handshakers