# actix_web::ws - Rust
API documentation for the Rust `ws` mod in crate `actix_web`.
Keywords: rust, rustlang, rust-lang, ws

# Module actix_web::ws [−] [src]

`WebSocket` support for Actix

To setup a `WebSocket`, first do web socket handshake then on success convert `Payload` into a `WsStream` stream and then use `WsWriter` to communicate with the peer.

## Example

    use actix_web::{ws, HttpRequest, HttpResponse};

    // do websocket handshake and start actor
    fn ws_index(req: &HttpRequest) -> Result<HttpResponse> {
        ws::start(req, Ws)
    }

    struct Ws;

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

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

## Structs

Client | `WebSocket` client
ClientHandshake | Future that implementes client websocket handshake process.
ClientReader | Websocket reader client
ClientWriter | Websocket writer client
CloseReason | Reason for closing the connection
Frame | A struct representing a `WebSocket` frame.
FramedMessage | `WebSocket` message with framing.
WebsocketContext | Execution context for `WebSockets` actors
WsStream | Maps `Payload` stream into stream of `ws::Message` items

## Enums

ClientError | Websocket client error
CloseCode | Status code used to indicate why an endpoint is closing the `WebSocket` connection.
HandshakeError | Websocket handshake errors
Message | `WebSocket` Message
OpCode | Operation codes as part of rfc6455.
ProtocolError | Websocket protocol errors

## Traits

WsWriter | Common writing methods for a websocket.

## Functions

handshake | Prepare `WebSocket` handshake response.
start | Do websocket handshake and start actor

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g. `str,u8` or `String,struct:Vec,test`)