12 releases

0.4.0 Feb 24, 2026
0.3.1 Jan 14, 2026
0.3.0 Jul 20, 2024
0.2.5 Mar 10, 2022
0.1.0-alpha.0 Apr 25, 2020

#8 in WebSocket

Download history 28016/week @ 2026-01-24 32505/week @ 2026-01-31 38211/week @ 2026-02-07 37575/week @ 2026-02-14 34262/week @ 2026-02-21 39023/week @ 2026-02-28 42324/week @ 2026-03-07 41230/week @ 2026-03-14 42332/week @ 2026-03-21 41793/week @ 2026-03-28 35764/week @ 2026-04-04 40338/week @ 2026-04-11 39399/week @ 2026-04-18 42194/week @ 2026-04-25 36322/week @ 2026-05-02 26671/week @ 2026-05-09

151,198 downloads per month
Used in 65 crates (43 directly)

MIT/Apache

85KB
1.5K SLoC

actix-ws

WebSockets for Actix Web, without actors.

crates.io Documentation Version MIT or Apache 2.0 licensed
Dependency Status Download Chat on Discord

Example

use actix_web::{middleware::Logger, web, App, HttpRequest, HttpServer, Responder};
use actix_ws::Message;

async fn ws(req: HttpRequest, body: web::Payload) -> actix_web::Result<impl Responder> {
    let (response, mut session, mut msg_stream) = actix_ws::handle(&req, body)?;

    actix_web::rt::spawn(async move {
        while let Some(Ok(msg)) = msg_stream.recv().await {
            match msg {
                Message::Ping(bytes) => {
                    if session.pong(&bytes).await.is_err() {
                        return;
                    }
                }
                Message::Text(msg) => println!("Got text: {msg}"),
                _ => break,
            }
        }

        let _ = session.close(None).await;
    });

    Ok(response)
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(move || {
        App::new()
            .wrap(Logger::default())
            .route("/ws", web::get().to(ws))
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await?;

    Ok(())
}

Typed Messages (Optional)

Enable the serde-json feature to send/receive typed messages using serde_json.

See examples/json.rs and run it with:

cargo run -p actix-ws --features serde-json --example json

WebSocket Sub-Protocols

Use handle_with_protocols when your server supports one or more Sec-WebSocket-Protocol values.

let (response, session, msg_stream) = actix_ws::handle_with_protocols(
    &req,
    body,
    &["graphql-transport-ws", "graphql-ws"],
)?;

When there is an overlap, the first protocol offered by the client that the server supports is returned in the handshake response.

Resources

License

This project is licensed under either of

at your option.

Dependencies

~19–34MB
~473K SLoC