ws-bridge
Strongly-typed WebSocket endpoints for Rust — define once, use everywhere.
Server (axum) · Browser client (yew/gloo) · Native client (tokio-tungstenite)
The problem
WebSocket projects in Rust tend to accumulate duplicated boilerplate:
- Path strings scattered across server routes and client connect calls
- Serde plumbing (
serde_json::to_string/from_str) at every send/recv - URL derivation (
ws://vswss://) copy-pasted in every frontend - Split + send task pattern repeated in every axum handler
- Reconnection logic reimplemented in every project
The solution
Define your endpoint once:
use WsEndpoint;
use ;
;
Use it everywhere with full type safety:
Server (axum)
use Router;
use WsEndpoint;
let app = new.route;
Native client (tokio-tungstenite)
let mut conn = .await?;
conn.send.await?;
while let Some = conn.recv.await
Browser client (yew/gloo)
let mut conn = ?;
conn.send.await?;
while let Some = conn.recv.await
Features
Enable only what you need:
| Feature | What it provides | Dependencies |
|---|---|---|
server |
axum WebSocket handlers | axum, tokio, futures-util |
yew-client |
Browser WebSocket client (WASM) | gloo-net, wasm-bindgen-futures, web-sys |
native-client |
tokio-tungstenite client | tokio-tungstenite, tokio, futures-util |
reconnect |
Exponential backoff reconnection | tokio, futures-util |
[]
# Server crate
= { = "0.1", = ["server"] }
# Frontend crate (WASM)
= { = "0.1", = ["yew-client"] }
# CLI / daemon crate
= { = "0.1", = ["native-client", "reconnect"] }
Key concepts
WsEndpoint— A trait you implement on a unit struct. DefinesPATH,ServerMsg, andClientMsg. This is the single source of truth shared between server and clients.WsCodec— Encode/decode trait. AnySerialize + DeserializeOwnedtype automatically gets JSON encoding. Implement manually for binary protocols.WsConnection<S, R>— Transport-agnostic typed connection.send()only acceptsS,recv()only yieldsR.NoMessages— Uninhabited type for unidirectional endpoints (e.g., server-push-only streams).
Documentation
- Server Guide — axum handlers, state extraction, pre-upgrade auth, manual upgrades
- Browser Client Guide — WASM/yew usage,
!Sendconstraints, Yew component patterns - Native Client Guide — tokio-tungstenite connections, TLS
- Reconnection Guide — Exponential backoff, custom connect functions
- Custom Codecs Guide — Binary protocols,
NoMessages, theWsCodectrait
Examples
Run the examples with:
# Terminal 1: start the server
# Terminal 2: connect a client
# Server with shared state
# Reconnecting client
License
MIT