Crate web_socket

source ·
Expand description


This library is an implementation of the WebSocket protocol, which provides a way for two-way communication between a client and server over a single TCP connection. This library provides fastest and intuitive WebSocket implementation for both client and server-side applications.


To use this library, add it as a dependency to your Rust project by adding the following line to your Cargo.toml file:

web-socket = "0.7"


You can run this example with: cargo run --example minimal

use tokio::io::*;
use web_socket::*;

async fn example<IO>(mut ws: WebSocket<IO>) -> Result<()>
    IO: Unpin + AsyncRead + AsyncWrite,
    for _ in 0..3 {
        ws.send("Copy Cat!").await?;

        match ws.recv_event().await? {
            Event::Data { ty, data } => {
                assert!(matches!(ty, DataType::Complete(MessageType::Text)));
                assert_eq!(&*data, b"Copy Cat!");
            Event::Ping(data) => ws.send_pong(data).await?,
            Event::Pong(..) => {}
            Event::Error(..) => return ws.close(CloseCode::ProtocolError).await,
            Event::Close { .. } => return ws.close(()).await,

For more examples, see ./examples directory.

It passed all test of the autobahn testsuite


This project is licensed under Apache License 2.0


  • WebSocket implementation for both client and server


  • When closing an established connection an endpoint MAY indicate a reason for closure.
  • Data that is either complete or fragmented.
  • Represent a websocket event
  • It represent the type of data that is being sent over the WebSocket connection.
  • Two roles that can be played by a WebSocket connection: Server and Client.
  • Represents a fragment of a WebSocket message.


  • This trait is responsible for encoding websocket closed frame.