stream-tungstenite
Overview
stream-tungstenite is a powerful Rust WebSocket client library designed for applications requiring reliable connections. It provides automatic reconnection, connection state management, extension mechanisms, and detailed metrics collection.
Core Features
- Intelligent Reconnection: Multiple reconnection strategies including exponential backoff algorithm
- Connection State Management: Real-time tracking of connection status and health
- Extension Mechanism: Support for registering custom extensions to handle specific business logic
- Event Streams: Provides message receiving streams and status change streams
- Metrics Collection: Detailed connection metrics and error statistics
- Configuration Presets: Pre-configured options for different scenarios
Quick Start
Basic Usage
use *;
use Arc;
async
Using Configuration Presets
use *;
use Arc;
async
Monitoring Connection Status
use *;
use StreamExt;
use Arc;
async
Advanced Configuration
Custom Reconnection Strategy
use *;
use Duration;
let custom_strategy = new.with_max;
let config = default
.with_exp_backoff_strategy
.with_receive_timeout;
let client = new;
Configuration Preset Options
The library provides three preset configurations for different scenarios (from src/config.rs):
1. Fast Reconnection - fast_reconnect()
Suitable for scenarios requiring quick connection recovery:
- Initial delay: 500ms
- Growth factor: 1.5
- Maximum delay: 10 seconds
- Receive timeout: 10 seconds
2. Stable Connection - stable_connection()
Suitable for scenarios requiring long-term stable connections:
- Initial delay: 2 seconds
- Growth factor: 2.0
- Maximum delay: 120 seconds
- Receive timeout: 60 seconds
3. Low Latency - low_latency()
Suitable for latency-sensitive scenarios:
- Initial delay: 100ms
- Growth factor: 1.2
- Maximum delay: 5 seconds
- Receive timeout: 5 seconds
Extension Mechanism
You can add custom functionality by implementing the Extension trait:
use *;
// Register extension
let client = new;
client.register_extension.await.unwrap;
Connection State and Metrics
Getting Connection State
// Get connection state snapshot (from src/tungstenite.rs)
let state = client.get_connection_state.await;
println!;
println!;
println!;
println!;
Health Check
// Check if connection is healthy
let is_healthy = client.is_connection_healthy;
println!;
Error Handling
The library provides structured error handling mechanisms (from src/errors.rs):
use *;
// Errors are automatically recorded in connection state
let state = client.get_connection_state.await;
if let Some = state.last_error
Reconnection Strategy Details
Exponential Backoff Strategy
ExpBackoffStrategy provides flexible exponential backoff reconnection mechanism (from src/strategies.rs):
use *;
use Duration;
let strategy = new
.with_max // Maximum delay not exceeding 60 seconds
.with_seed; // Optional: set random seed
let config = default
.with_exp_backoff_strategy;
Strategy Features
- Exponential Growth: Delay time grows exponentially to avoid frequent retries
- Jitter Mechanism: Add random jitter to avoid multiple clients retrying simultaneously
- Maximum Delay: Set delay ceiling to prevent excessive wait times
- Resettable: Support resetting strategy state to start over
Installation
Add dependency to your Cargo.toml:
[]
= "0.4.0"
= { = "1.0", = ["full"] }
License
This project is licensed under the Apache License Version 2.0. See the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit Pull Requests or create Issues to report bugs and suggest improvements.