websockets-monoio
A high-performance WebSocket client for the monoio async runtime. It dials both ws:// and wss:// endpoints, performs the HTTP upgrade handshake, and hands you a fully configured [fastwebsockets_monoio::WebSocket] client stream.
Note: Documentation is AI generated.
Highlights
- Monoio-first: Uses
io_uringon Linux via monoio for low-latency networking. - TLS out of the box:
wss://connections usemonoio-rustlswith the Mozilla root store. - Zero-copy friendly: Frame writes avoid intermediate allocations whenever possible.
- Safe defaults: Auto close and auto pong are enabled; TLS writev is disabled for compatibility.
- Minimal surface area: One
WsClient::connecthelper plus re-exported stream types if you want lower-level control.
Install
Add the crate and its companion dependencies to your project:
[]
= "0.1.1"
= "0.2"
= "0.10"
= "1.0"
The crate targets Rust 1.90.0 or newer (see Cargo.toml).
Usage
Basic WebSocket connection
use ;
use WsClient;
async
Streaming example
use ;
use WsClient;
async
Custom request headers
use WsClient;
async
Further examples live in examples/:
cargo run --example echo_clientcargo run --example crypto_stream
API overview
WsClient::connect(url, extra_headers)performs DNS resolution, TCP/TLS setup, and the HTTP upgrade handshake before returning aWebSocket<WsStream>.WsClient::into_inner()gives direct access to the underlyingfastwebsockets_monoio::WebSocket.WsStreamis the enum used by the client (PlainTCP orTlsover TCP). It implementsmonoio_compat::AsyncReadandAsyncWrite.- Supporting modules such as
http_upgrade,tls, andurlare re-exported for advanced use-cases if you want to build your own handshake flow.
Errors from WsClient::connect use anyhow::Result, allowing full context while still being compatible with other error handling strategies.
Benchmarks
Benchmarks live in benches/perf.rs and run locally without external services. Launch them with:
What you get:
connect/ws_connectmeasures full handshake latency against an in-process monoio echo server.round_trip/*tests send-and-receive latency for text and binary frames of varying sizes.
Results depend on kernel support for io_uring; Linux 5.1+ is recommended for representative numbers.
Platform notes
- Linux: Full support with
io_uring. This is the primary target. - macOS / Windows: Works via monoio’s fallback driver, but without
io_uringoptimisations.
TLS connections use rustls with the Mozilla CA bundle (webpki-roots). A global TlsConnector is reused across calls to keep setup cheap.
Contributing
Issues and PRs are welcome. By contributing you agree to license your work under MIT OR Apache-2.0, the same as the rest of the project.
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.