Skip to main content

Crate yawc

Crate yawc 

Source
Expand description

§yawc

WebSocket (RFC 6455) with permessage-deflate compression (RFC 7692). Autobahn compliant. Supports WASM targets.

§Features

  • reqwest: WebSocket via reqwest HTTP client
  • axum: WebSocket extractor for axum
  • zlib: Advanced compression with window size control
  • json: 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
codecNon-WebAssembly
codec
frame
Frame
streamingNon-WebAssembly
Low-level streaming WebSocket layer for manual fragment control.

Structs§

DeflateOptionsNon-WebAssembly
Configuration options for WebSocket message compression using the Deflate algorithm.
FragmentationNon-WebAssembly
Configuration for WebSocket message fragmentation.
IncomingUpgradeaxum
Represents an incoming WebSocket upgrade request that can be converted into a WebSocket connection.
OptionsNon-WebAssembly
Configuration options for a WebSocket connection.
ReadHalfNon-WebAssembly
The read half of a WebSocket connection, responsible for receiving and processing incoming messages.
UpgradeFutNon-WebAssembly
Future that completes the WebSocket upgrade process on a server, returning a WebSocket stream.
WebSocketNon-WebAssembly
WebSocket stream for both clients and servers.
WebSocketBuilderNon-WebAssembly
Builder for establishing WebSocket connections with customizable options.
WriteHalfNon-WebAssembly
Write half of the WebSocket connection.

Enums§

HttpStreamNon-WebAssembly
An enum representing the underlying WebSocket stream types based on the enabled features.
MaybeTlsStreamNon-WebAssembly
A stream that might be protected with TLS.
RoleNon-WebAssembly
The role the WebSocket stream is taking.
WebSocketError
Errors that can occur during WebSocket operations.

Constants§

MAX_PAYLOAD_READNon-WebAssembly
The maximum allowed payload size for reading, set to 1 MiB.
MAX_READ_BUFFERNon-WebAssembly
The maximum allowed read buffer size, set to 2 MiB.

Type Aliases§

CompressionLevelNon-WebAssembly
Type alias for the compression level used in WebSocket compression settings.
HttpRequestNon-WebAssembly
Type alias for HTTP requests used in WebSocket connection handling.
HttpRequestBuilderNon-WebAssembly
Type alias for HTTP request builders used in WebSocket client connection setup.
HttpResponseNon-WebAssembly
Type alias for HTTP responses used during WebSocket upgrade.
HttpWebSocketNon-WebAssembly
Type alias for server-side WebSocket connections from HTTP upgrades or when using reqwest.
Result
Result type for WebSocket operations.
TcpWebSocketNon-WebAssembly
Type alias for WebSocket connections established via connect.
UpgradeResultNon-WebAssembly
The result type returned by WebSocket upgrade operations.