31 releases (11 stable)

4.3.0 Feb 4, 2024
4.2.0 Jan 21, 2023
4.1.0 Mar 2, 2022
4.0.0-beta.9 Dec 27, 2021
1.0.0-alpha.2 Mar 30, 2019

#1043 in Network programming

Download history 22816/week @ 2024-01-23 24536/week @ 2024-01-30 25059/week @ 2024-02-06 23361/week @ 2024-02-13 23051/week @ 2024-02-20 23309/week @ 2024-02-27 21932/week @ 2024-03-05 22548/week @ 2024-03-12 23161/week @ 2024-03-19 19603/week @ 2024-03-26 25430/week @ 2024-04-02 22614/week @ 2024-04-09 22382/week @ 2024-04-16 23514/week @ 2024-04-23 19502/week @ 2024-04-30 15843/week @ 2024-05-07

85,110 downloads per month
Used in 113 crates (82 directly)

MIT/Apache

1.5MB
33K SLoC

actix-web-actors

Actix actors support for Actix Web.

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

Documentation & Resources


lib.rs:

Actix actors support for Actix Web.

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

~16–28MB
~509K SLoC