Skip to content

Commit

Permalink
Add WebSocket section to upgrading guide.
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioBenitez committed Nov 17, 2023
1 parent f6916e6 commit daa4e34
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
43 changes: 43 additions & 0 deletions site/guide/01-upgrading.md
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,49 @@ fn stream(n: Option<u64>) -> EventStream![] {
[async streams]: ../responses/#async-streams
[chat example]: @example/chat

### WebSockets

Rocket v0.5 introduces support for HTTP connection upgrades via a new [upgrade
API]. The API allows responders to take over an HTTP connection and perform raw
I/O with the client. In other words, an HTTP connection can be _upgraded_ to any
protocol, including HTTP WebSockets!

The newly introduced [`rocket_ws`] library takes advantage of the new API to
implement first-class support for WebSockets entirely outside of Rocket's core.
The simplest use of the library, implementing an echo server and showcasing that
the incoming message stream is `async`, looks like this:

```rust
# use rocket::get;
# use rocket_ws as ws;

#[get("/echo")]
fn echo_compose(ws: ws::WebSocket) -> ws::Stream!['static] {
ws.stream(|io| io)
}
```

The simplified [async streams] generator syntax can also be used:

```rust
# use rocket::get;
# use rocket_ws as ws;

#[get("/echo")]
fn echo_stream(ws: ws::WebSocket) -> ws::Stream!['static] {
ws::Stream! { ws =>
for await message in ws {
yield message?;
}
}
}
```

For complete usage details, see the [`rocket_ws`] documentation.

[upgrade API]: @api/rocket/response/struct.Response.html#upgrading
[`rocket_ws`]: @api/rocket_ws

## Getting Help

If you run into any issues upgrading, we encourage you to ask questions via
Expand Down
3 changes: 3 additions & 0 deletions site/tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ features = ["tera"]
[dev-dependencies.rocket_db_pools]
path = "../../contrib/db_pools/lib"
features = ["sqlx_sqlite"]

[dev-dependencies.rocket_ws]
path = "../../contrib/ws/lib"

0 comments on commit daa4e34

Please sign in to comment.