Expand description
§yawc
WebSocket (RFC 6455) with permessage-deflate compression (RFC 7692). Autobahn compliant. Supports WASM targets.
§Features
reqwest: WebSocket via reqwest HTTP clientaxum: WebSocket extractor for axumzlib: Advanced compression with window size controljson: JSON serialization support
§Runtime Support
yawc is built on tokio’s I/O traits but can work with other async runtimes through simple adapters.
While the library uses tokio internally for its codec and I/O operations, you can integrate it with
runtimes like smol, async-std, or others by implementing trait bridges between their I/O traits
and tokio’s AsyncRead/AsyncWrite.
See the client_smol.rs example for a complete demonstration of using yawc with the smol runtime.
§Client Example
use futures::{SinkExt, StreamExt};
use yawc::{WebSocket, frame::OpCode};
async fn connect() -> yawc::Result<()> {
let mut ws = WebSocket::connect("wss://echo.websocket.org".parse()?).await?;
while let Some(frame) = ws.next().await {
match frame.opcode() {
OpCode::Text | OpCode::Binary => ws.send(frame).await?,
OpCode::Ping => {
// Pong is sent automatically, but ping is still returned
// so you can observe it if needed
}
_ => {}
}
}
Ok(())
}§Protocol Handling
yawc automatically handles WebSocket control frames:
- Ping frames: Automatically responded to with pongs, but still returned to your application
- Pong frames: Passed through without special handling
- Close frames: Automatically acknowledged, then returned before closing the connection
§Server Example
use http_body_util::Empty;
use futures::StreamExt;
use hyper::{Request, body::{Incoming, Bytes}, Response};
use yawc::WebSocket;
async fn upgrade(mut req: Request<Incoming>) -> yawc::Result<Response<Empty<Bytes>>> {
let (response, fut) = WebSocket::upgrade(&mut req)?;
tokio::spawn(async move {
if let Ok(mut ws) = fut.await {
while let Some(frame) = ws.next().await {
// Process frames
}
}
});
Ok(response)
}Modules§
- close
- codec
Non-WebAssembly - codec
- frame
- Frame
- streaming
Non-WebAssembly - Low-level streaming WebSocket layer for manual fragment control.
Structs§
- Deflate
Options - Configuration options for WebSocket message compression using the Deflate algorithm.
- Fragmentation
- Configuration for WebSocket message fragmentation.
- Incoming
Upgrade axum - Represents an incoming WebSocket upgrade request that can be converted into a WebSocket connection.
- Options
- Configuration options for a WebSocket connection.
- Read
Half - The read half of a WebSocket connection, responsible for receiving and processing incoming messages.
- Upgrade
Fut - Future that completes the WebSocket upgrade process on a server, returning a WebSocket stream.
- WebSocket
Non-WebAssembly - WebSocket stream for both clients and servers.
- WebSocket
Builder - Builder for establishing WebSocket connections with customizable options.
- Write
Half - Write half of the WebSocket connection.
Enums§
- Http
Stream Non-WebAssembly - An enum representing the underlying WebSocket stream types based on the enabled features.
- Maybe
TlsStream - A stream that might be protected with TLS.
- Role
Non-WebAssembly - The role the WebSocket stream is taking.
- WebSocket
Error - Errors that can occur during WebSocket operations.
Constants§
- MAX_
PAYLOAD_ READ Non-WebAssembly - The maximum allowed payload size for reading, set to 1 MiB.
- MAX_
READ_ BUFFER Non-WebAssembly - The maximum allowed read buffer size, set to 2 MiB.
Type Aliases§
- Compression
Level - Type alias for the compression level used in WebSocket compression settings.
- Http
Request - Type alias for HTTP requests used in WebSocket connection handling.
- Http
Request Builder - Type alias for HTTP request builders used in WebSocket client connection setup.
- Http
Response Non-WebAssembly - Type alias for HTTP responses used during WebSocket upgrade.
- Http
WebSocket Non-WebAssembly - Type alias for server-side WebSocket connections from HTTP upgrades or when using reqwest.
- Result
- Result type for WebSocket operations.
- TcpWeb
Socket Non-WebAssembly - Type alias for WebSocket connections established via
connect. - Upgrade
Result Non-WebAssembly - The result type returned by WebSocket upgrade operations.