2018-05-22 23:15:08 +02:00
---
2024-08-19 03:21:54 +02:00
title: WebSockets
2018-05-22 23:15:08 +02:00
---
2024-05-27 22:55:31 +02:00
import CodeBlock from "@site/src/components/code_block";
2022-07-16 11:59:20 +02:00
2024-08-19 03:21:54 +02:00
# WebSockets
2022-07-16 11:59:20 +02:00
2024-08-19 03:21:54 +02:00
Actix Web supports a high-level WebSocket interface via the `actix-ws` crate. Using this crate, it's possible to convert a request's `Payload` stream into a stream of [_ws::Message_][message]s and then react to them inside a spawned async task.
2018-05-22 23:15:08 +02:00
2024-08-19 03:21:54 +02:00
The following is an example of a simple WebSocket echo server:
2018-05-22 23:15:08 +02:00
2022-07-16 11:59:20 +02:00
< CodeBlock example = "websockets" file = "main.rs" section = "websockets" / >
2018-05-22 23:15:08 +02:00
2024-08-19 03:21:54 +02:00
> A simple WebSocket echo server example is available [in the examples repo][echo].
2018-05-22 23:15:08 +02:00
2024-08-19 03:21:54 +02:00
> An example chat server is also available [in the examples directory][chat]
2019-06-25 05:36:32 +02:00
2024-08-19 03:21:54 +02:00
[message]: https://docs.rs/actix-ws/0.3/actix_ws/enum.Message.html
2022-03-06 00:55:35 +01:00
[payload]: https://docs.rs/actix-web/4/actix_web/web/struct.Payload.html
2024-08-19 03:21:54 +02:00
[echo]: https://github.com/actix/examples/tree/master/websockets/echo-actorless
[chat]: https://github.com/actix/examples/tree/master/websockets/chat-actorless