wscall
WSCALL is a lightweight WebSocket API framework with a custom binary frame protocol, a reusable Rust server crate, a reusable Rust client crate, a JavaScript client SDK, and a facade crate for consumers that prefer a single dependency.
Workspace Layout
The repository is now organized as a Cargo workspace:
crates/
wscall-protocol/
wscall-server/
wscall-client/
wscall/
Each crate has a clear responsibility:
wscall-protocol: shared frame codec, envelope types, file attachment model, and protocol errors.wscall-server: reusable WebSocket server framework with routes, filters, validation, exception mapping, and server push events.wscall-client: reusable client SDK with request correlation, event ACK correlation, and server event subscriptions.wscall: facade crate that re-exports protocol plus optional server and client APIs behind features.
Protocol Summary
Each WebSocket binary message is encoded as:
| frame_len:u32 | message_type:u8 | encryption:u8 | payload |
In protocol v2 the payload is a binary composite frame:
| meta_len:u32 | JSON envelope bytes | att_count:u8 | [attachment binary sections] |
Transport behavior:
- Plaintext mode stores the composite frame directly in
payload. ChaCha20andAES256modes store12-byte nonce + ciphertextinpayload.- The whole-frame size limit is configurable via
with_max_frame_bytes(default100 MiB); oversized inbound frames get a413(frame_too_large) error response without closing the connection. - File parameters use
{"$file": "<id>"}JSON references; attachment payloads travel as raw binary sections (no Base64 overhead). - The JSON envelope uses compact single-letter keys and a numeric
kdiscriminator to minimize per-frame overhead. request_id/event_idare per-connectionu64counters serialized as JSON numbers (1–6 bytes).connection_iduses UUIDv7 (time-ordered, generated once per connection).- Event
data(d) is always a JSON object;metadata(m) is optional and omitted when empty.
Key Agreement Modes
The framework supports two key modes, determined at connection time:
PSK (Pre-Shared Key) — all connections share a single ChaCha20-Poly1305 or AES-256-GCM key configured on both sides. Suitable for trusted environments where keys can be pre-distributed.
ECDH Dynamic Key Agreement — based on X25519, each connection negotiates a unique 32-byte ChaCha20-Poly1305 session key via an in-band handshake immediately after the WebSocket upgrade. The session key is never transmitted over the wire. This mode provides forward secrecy and is suitable for zero-trust deployments.
- Server:
WscallServer::new().with_ecdh() - Rust client:
WscallClient::connect(url, WscallClientConfig::ecdh()) - JS client:
WscallClient.connect(url, WscallClientConfig.ecdh())
The handshake exchanges 32-byte raw public keys as binary WebSocket messages, then derives SHA-256("wscall-ecdh-v1" || shared_secret) as the session key.
Use As Crates
Depend on only what you need:
[]
= "0.5.0"
= "0.5.0"
Or use the facade crate:
[]
= { = "0.5.0", = ["full"] }
Quick Start
Minimal server:
use json;
use WscallServer;
async
Minimal client:
use json;
use ;
async
ECDH client (recommended default, no pre-shared key needed):
let client = connect.await?;
Failover across multiple servers:
let config = ecdh
.with_failover_url
.with_failover_url;
let client = connect.await?;
Client reconnect behavior:
- Unexpected disconnects trigger automatic reconnect attempts (controlled by
WscallClientConfig.auto_reconnect, defaulttrue). - The first retry waits 3 seconds.
- Each later retry doubles the delay (exponential backoff: 3s → 6s → 12s …).
- The retry delay is capped at 30 seconds.
- A random sub-second jitter is added to each retry to avoid thundering-herd reconnect storms.
- Calling
close()stops reconnect attempts. - Use
WscallClientConfig::default().with_auto_reconnect(false)to disable automatic reconnection. - When
failover_urlsis configured, each reconnect cycle iterates through all URLs (starting from the last successful one) before applying backoff delay.
Runnable end-to-end quick start:
Run The Demos
Start the demo server:
# PSK (ChaCha20) mode
# ECDH mode
In another terminal, run the demo client:
# PSK (ChaCha20) mode
# ECDH mode
A JavaScript demo client is also available in the wscall-client-js repository:
# PSK mode
# ECDH mode
The demos exercise:
system.echoAPI.files.inspectAPI with inline attachment.chat.messageevent emit and broadcast.chat.historyAPI query.- End-to-end ChaCha20 frame encryption using the demo key wired in both examples.
- ECDH mode (
--ecdhflag) demonstrates dynamic per-connection key agreement without any pre-shared key.
Performance Highlights
- Concurrent request handling: each inbound API request/event runs in its own
tokio::spawntask with a per-connection semaphore (default 64 in-flight), eliminating head-of-line blocking. - Zero-copy broadcast:
broadcast_eventencodes the frame once and shares it asBytesacross all recipients. - Pre-encoded outbound: all frames are encoded at dispatch time; the writer task only ships bytes.
- Lock-free hot paths:
DashMapconnection table,ArcSwapOptionwriter handle,DashMap<u64>pending correlation map. - Cached ciphers: AES-256-GCM and ChaCha20-Poly1305 key schedules are computed once and shared via
Arc. - Compact wire format: single-letter JSON keys + numeric
ktag +u64counters for IDs minimize per-frame byte overhead. - Low latency:
TCP_NODELAYon accept,tracinginstead ofprintln!on hot paths. - Forward secrecy via ECDH: each connection negotiates a unique X25519 session key; reconnects generate fresh keys automatically.
- Zero-copy typed routing:
typed_route/validated_routemove params out of the context instead of deep-cloning the JSON tree per request. - Single-buffer codec: frames are encoded in a single buffer and decoded in a single JSON pass, minimizing per-frame allocations and copies.
- Concurrent lifecycle hooks: server
on_connected/on_disconnectedhandlers run concurrently and never delay connection setup.
See framework-instruction.md for the full architecture and performance model documentation.
Quality Gates
Recommended checks before publishing or merging:
Publishing Checklist
Before publishing the crates to crates.io:
- Confirm the configured
repositoryandhomepagemetadata still match the canonical repository. - Run the quality gates locally.
- Update all workspace crate versions and internal dependency pins to the target release version.
- Run
cargo packageforwscall-protocolto validate the root dependency crate. - Publish in dependency order:
wscall-protocol,wscall-server,wscall-client, thenwscall. - After each publish, wait for the crates.io index to catch up before packaging or publishing the next dependent crate.
For any release that introduces a new unpublished dependency version, downstream crates such as wscall-server, wscall-client, and wscall must still be published in dependency order so crates.io can resolve the freshly published versions.
See RELEASE.md for a release sequence that matches this dependency chain.
Version history is tracked in CHANGELOG.md.
Current Constraints
ChaCha20andAES256-GCMare implemented inFrameCodec; the demos default toChaCha20.ECDHmode uses X25519 viax25519-dalek(Rust) and@noble/curves(JS); session keys are alwaysChaCha20-Poly1305.- Attachments are inline binary sections (protocol v2) suited to small and medium files.
- Large-file chunking is not part of the current core protocol.
- The published crate and code identifiers have been renamed to
wscall. - JavaScript client SDK is available at
wscall-client-js; it supports PSK and ECDH modes.