https://github.com/codebam/tokio-chat-server
https://github.com/codebam/tokio-chat-server
Last synced: 6 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/codebam/tokio-chat-server
- Owner: codebam
- License: gpl-3.0
- Created: 2025-06-27T09:27:39.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2025-06-29T04:48:31.000Z (9 months ago)
- Last Synced: 2025-09-26T08:49:02.712Z (6 months ago)
- Language: Rust
- Size: 104 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# tokio-chat-server
A high-performance WebSocket chat server built with Tokio, supporting both plain HTTP and secure TLS/WSS connections.
## Features
- **High Performance**: 19k-68k+ messages/second throughput
- **Fan-out Efficiency**: 80-100% message delivery success rate
- **Low Latency**: Sub-millisecond batching with smart flush strategies
- **Massive Scalability**: Successfully tested up to 100 concurrent connections with high fan-out
- **TLS/WSS Support**: Secure WebSocket connections with native-tls
- **Redis Integration**: Horizontal scaling with Redis pub/sub for distributed deployments
- **Dual Protocol**: Supports both `ws://` and `wss://` connections
- **Client Metrics**: Real-time tracking of message counts and activity
## Performance Benchmarks
**Latest Test Results on 16-core system:**
### HTTP WebSocket Performance (ws://)
| Test Scenario | Throughput | Delivery Rate | Details |
|---------------|------------|---------------|---------|
| Multicore High Throughput | **42,370 msg/sec** | 80.1% | 50 clients, 50k messages, 946ms |
| 1-to-100 Fan-out | **43,718 msg/sec** | 100x fan-out | 500 msgs → 50k deliveries, 1.14s |
| Burst Fan-out | **54,982 msg/sec** | 75x fan-out | 1k msgs → 75k deliveries, 1.36s |
| Complex Multi-sender | **4,419 msg/sec** | 44.5x fan-out | 10 senders → 50 receivers, 20.1s |
### TLS WebSocket Performance (wss://)
| Test Scenario | Throughput | Delivery Rate | Details |
|---------------|------------|---------------|---------|
| Simple Throughput (TLS) | **40,074 msg/sec** | 80.0% | 10k messages, 200ms |
| WebSocket High Throughput (TLS) | **68,288 msg/sec** | 99x fan-out | 10k msgs → 990k deliveries, 14.5s |
### Redis-Backed Performance (wss:// + Redis Pub/Sub)
| Test Scenario | Throughput | Delivery Rate | Details |
|---------------|------------|---------------|---------|
| Redis High Throughput (TLS) | **19,042 msg/sec** | 49x fan-out | 5k msgs → 245k deliveries, 12.9s |
**Performance Summary:**
- **Peak HTTP Throughput**: 54,982 messages/second
- **Peak TLS Throughput**: 68,288 messages/second
- **Redis-Backed Throughput**: 19,042 messages/second
- **TLS Massive Fan-out**: 99x message amplification with 100 clients
- **Redis Fan-out**: 49x message amplification with 50 clients
- **Fan-out Multiplier**: Up to 100x message amplification
- **Burst Send Rate**: 93,643 messages/second
- **Connection Scaling**: 100 concurrent TLS connections tested successfully
## Quick Start
### Basic HTTP Server
```rust
use chat_server::run_chat_server;
use tokio::net::TcpListener;
#[tokio::main]
async fn main() -> Result<(), Box> {
let listener = TcpListener::bind("127.0.0.1:8080").await?;
println!("Chat server running on ws://127.0.0.1:8080");
run_chat_server(listener).await;
Ok(())
}
```
### TLS/WSS Server
```rust
use chat_server::{run_chat_server_tls, create_test_tls_acceptor};
use tokio::net::TcpListener;
#[tokio::main]
async fn main() -> Result<(), Box> {
let listener = TcpListener::bind("127.0.0.1:8443").await?;
let tls_acceptor = create_test_tls_acceptor()?;
println!("Secure chat server running on wss://127.0.0.1:8443");
run_chat_server_tls(listener, tls_acceptor).await;
Ok(())
}
```
### Redis-Backed Server
```rust
use chat_server::{run_chat_server_tls_redis, create_test_tls_acceptor};
use tokio::net::TcpListener;
#[tokio::main]
async fn main() -> Result<(), Box> {
let listener = TcpListener::bind("127.0.0.1:8443").await?;
let tls_acceptor = create_test_tls_acceptor()?;
println!("Redis-backed chat server running on wss://127.0.0.1:8443");
run_chat_server_tls_redis(listener, tls_acceptor, "redis://127.0.0.1:6379").await;
Ok(())
}
```
## Testing
Run all tests including TLS functionality:
```bash
cargo test
```
**Test Results: 16/16 tests passing**
- 8 core functionality tests (including 2 TLS tests)
- 3 fan-out performance tests
- 2 multicore performance tests
- 1 simple throughput test
- 2 WebSocket performance tests