WebSocket in Rust: Advanced Real-Time Applications

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

WebSocket in Rust: Advanced Real-Time Applications

\n\n

Rust is an ideal language for building high-performance real-time applications thanks to its safety and zero-cost abstractions. In this article, we will explore advanced techniques for working with WebSocket: from scaling to authorization and error handling. You will learn how to build a production-ready WebSocket server capable of handling thousands of concurrent connections.

\n\n

WebSocket Server Architecture with Tokio and Tungstenite

\n\n

To build a reliable server, we use the combination of tokio (async runtime) and tokio-tungstenite (WebSocket library). The advanced architecture includes a connection pool, graceful shutdown, and error handling.

\n\n
use tokio::net::TcpListener;\nuse tokio_tungstenite::accept_async;\nuse futures_util::{SinkExt, StreamExt};\nuse std::sync::Arc;\nuse tokio::sync::Mutex;\n\ntype PeerMap = Arc<Mutex<HashMap<String, PeerConnection>>>;\n\n#[derive(Clone)]\nstruct PeerConnection {\n    sender: mpsc::UnboundedSender<Message>,\n    user_id: String,\n}\n\nasync fn handle_connection(\n    peer_map: PeerMap,\n    raw_stream: TcpStream,\n    addr: SocketAddr,\n) {\n    let ws_stream = accept_async(raw_stream)\n        .await\n        .expect("Error during handshake");\n    \n    let (mut write, mut read) = ws_stream.split();\n    let (tx, mut rx) = mpsc::unbounded_channel::<Message>();\n    \n    // Registration in the pool\n    let user_id = Uuid::new_v4().to_string();\n    peer_map.lock().await.insert(\n        user_id.clone(),\n        PeerConnection { sender: tx, user_id: user_id.clone() }\n    );\n    \n    // Spawning tasks\n    let send_task = tokio::spawn(async move {\n        while let Some(msg) = rx.recv().await {\n            if write.send(msg).await.is_err() {\n                break;\n            }\n        }\n    });\n    \n    let recv_task = tokio::spawn(async move {\n        while let Some(Ok(msg)) = read.next().await {\n            broadcast_message(&peer_map, &user_id, &msg).await;\n        }\n    });\n    \n    // Graceful shutdown\n    tokio::select! {\n        _ = send_task => {},\n        _ = recv_task => {},\n    }\n    \n    peer_map.lock().await.remove(&user_id);\n    println!("Client {} disconnected", addr);\n}\n
\n\n

This code demonstrates the basic architecture: a connection pool, duplex communication, and automatic removal upon disconnection.

\n\n

Scaling: Thousands of Connections and Clustering

\n\n

To handle thousands of connections, we use several key patterns:

\n\n
    \n
  • Horizontal scaling — running multiple server instances with a load balancer
  • \n
  • Redis Pub/Sub — message exchange between instances
  • \n
  • Sharding — distribution by user_id
  • \n
\n\n

Example of integration with Redis for inter-server communication:

\n\n
use redis::AsyncCommands;\nuse serde::{Serialize, Deserialize};\n\n#[derive(Serialize, Deserialize)]\nstruct ClusterMessage {\n    from: String,\n    to: String,\n    payload: String,\n    timestamp: u64,\n}\n\nasync fn handle_cluster_message(\n    redis_conn: &mut redis::aio::Connection,\n    peer_map: PeerMap,\n    channel: String,\n) {\n    let mut pubsub = redis_conn.as_pubsub();\n    pubsub.subscribe(channel).await.unwrap();\n    \n    let mut stream = pubsub.on_message();\n    \n    while let Some(msg) = stream.next().await {\n        let payload: String = msg.get_payload().unwrap();\n        let cluster_msg: ClusterMessage = serde_json::from_str(&payload).unwrap();\n        \n        // Send to local client\n        if let Some(peer) = peer_map.lock().await.get(&cluster_msg.to) {\n            let _ = peer.sender.send(Message::Text(cluster_msg.payload.into()));\n        }\n    }\n}\n
\n\n

For load distribution, we use consistent hashing. This ensures even distribution with

Blogs

Book Recommendations