32 releases (12 stable)

4.3.1+deprecated Aug 7, 2024
4.3.0 Feb 4, 2024
4.2.0 Jan 21, 2023
4.1.0 Mar 2, 2022
1.0.0-alpha.2 Mar 30, 2019

#31 in #actix-actor

Download history 31386/week @ 2024-08-25 33313/week @ 2024-09-01 32205/week @ 2024-09-08 24791/week @ 2024-09-15 28949/week @ 2024-09-22 30023/week @ 2024-09-29 24487/week @ 2024-10-06 28562/week @ 2024-10-13 28887/week @ 2024-10-20 25614/week @ 2024-10-27 25543/week @ 2024-11-03 23915/week @ 2024-11-10 29023/week @ 2024-11-17 28817/week @ 2024-11-24 26292/week @ 2024-12-01 27560/week @ 2024-12-08

112,718 downloads per month
Used in 121 crates (86 directly)

MIT/Apache

1.5MB
33K SLoC

actix-web-actors

Actix actors support for Actix Web.

This crate is deprecated. Migrate to actix-ws.

crates.io Documentation Version License
dependency status Download Chat on Discord


lib.rs:

Actix actors support for Actix Web.

This crate is deprecated. Migrate to actix-ws.

Examples

use actix::{Actor, StreamHandler};
use actix_web::{get, web, App, Error, HttpRequest, HttpResponse, HttpServer};
use actix_web_actors::ws;

/// Define Websocket actor
struct MyWs;

impl Actor for MyWs {
    type Context = ws::WebsocketContext<Self>;
}

/// Handler for ws::Message message
impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for MyWs {
    fn handle(&mut self, msg: Result<ws::Message, ws::ProtocolError>, ctx: &mut Self::Context) {
        match msg {
            Ok(ws::Message::Ping(msg)) => ctx.pong(&msg),
            Ok(ws::Message::Text(text)) => ctx.text(text),
            Ok(ws::Message::Binary(bin)) => ctx.binary(bin),
            _ => (),
        }
    }
}

#[get("/ws")]
async fn index(req: HttpRequest, stream: web::Payload) -> Result<HttpResponse, Error> {
    ws::start(MyWs, &req, stream)
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| App::new().service(index))
        .bind(("127.0.0.1", 8080))?
        .run()
        .await
}

Documentation & Community Resources

In addition to this API documentation, several other resources are available:

To get started navigating the API docs, you may consider looking at the following pages first:

  • [ws]: This module provides actor support for WebSockets.

  • HttpContext: This struct provides actor support for streaming HTTP responses.

Dependencies

~14–26MB
~415K SLoC