9 releases
| new 0.1.12 | Jan 18, 2026 |
|---|---|
| 0.1.11 | Jan 14, 2026 |
#240 in WebSocket
Used in rustapi-rs
2.5MB
16K
SLoC
rustapi-ws
WebSocket support for the RustAPI framework.
This crate provides WebSocket upgrade handling, message types, and utilities for building real-time bidirectional communication in your RustAPI applications.
Features
- WebSocket Upgrade: Seamless HTTP to WebSocket upgrade via the
WebSocketextractor - Message Types: Support for Text, Binary, Ping/Pong messages
- Type-Safe JSON: Serialize/deserialize JSON messages with serde
- Connection Management: Clean connection lifecycle with proper close handling
- Broadcast Support: Send messages to multiple connected clients
Quick Start
use rustapi_rs::prelude::*;
use rustapi_ws::{WebSocket, Message};
async fn ws_handler(ws: WebSocket) -> impl IntoResponse {
ws.on_upgrade(|socket| async move {
let (mut sender, mut receiver) = socket.split();
while let Some(msg) = receiver.next().await {
match msg {
Ok(Message::Text(text)) => {
let _ = sender.send(Message::Text(format!("Echo: {}", text))).await;
}
Ok(Message::Close(_)) => break,
_ => {}
}
}
})
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
RustApi::new()
.route("/ws", get(ws_handler))
.run("127.0.0.1:8080")
.await
}
RustAPI WebSocket
Real-time bidirectional communication made simple.
Built on tokio-tungstenite, this crate provides a first-class WebSocket extractor for RustAPI.
Usage
use rustapi_ws::{WebSocket, Message};
#[get("/chat")]
async fn chat_handler(ws: WebSocket) -> impl Responder {
ws.on_upgrade(handle_socket)
}
async fn handle_socket(mut socket: WebSocket) {
while let Some(Ok(msg)) = socket.recv().await {
if let Message::Text(text) = msg {
println!("Received: {}", text);
socket.send(Message::Text("Echo!".into())).await.unwrap();
}
}
}
Features
- Auto-Upgrade: Handles the HTTP 101 Switching Protocols handshake.
- Channels: Built-in pub/sub for broadcast scenarios (chat rooms).
- Ping/Pong: Automatic heartbeat management.
Dependencies
~28MB
~419K SLoC