#http-request #stream

reqwest-websocket

WebSocket connections with reqwest

9 unstable releases

new 0.5.0 May 19, 2025
0.4.4 Dec 15, 2024
0.4.3 Oct 24, 2024
0.4.1 Jul 9, 2024
0.2.0 Feb 21, 2024

#17 in WebSocket

Download history 2446/week @ 2025-02-02 2918/week @ 2025-02-09 3128/week @ 2025-02-16 3296/week @ 2025-02-23 4463/week @ 2025-03-02 3751/week @ 2025-03-09 3161/week @ 2025-03-16 4223/week @ 2025-03-23 4377/week @ 2025-03-30 5302/week @ 2025-04-06 4170/week @ 2025-04-13 4133/week @ 2025-04-20 3771/week @ 2025-04-27 4495/week @ 2025-05-04 5198/week @ 2025-05-11 4767/week @ 2025-05-18

18,505 downloads per month
Used in 21 crates (10 directly)

MIT license

53KB
969 lines

reqwest-websocket

crates.io Documentation MIT Build

Extension for reqwest to allow websocket connections.

This crate contains the extension trait RequestBuilderExt, which adds an upgrade method to reqwest::RequestBuilder that prepares the HTTP request to upgrade the connection to a WebSocket. After you call upgrade(), you can send your upgraded request as usual with send(), which will return an UpgradeResponse. The UpgradeResponse wraps reqwest::Response (and also dereferences to it), so you can inspect the response if needed. Finally, you can use into_websocket() on the response to turn it into an async stream and sink for messages. Both text and binary messages are supported.

Example

For a full example take a look at hello_world.rs.

// Extends the `reqwest::RequestBuilder` to allow WebSocket upgrades.
use reqwest_websocket::RequestBuilderExt;

// Creates a GET request, upgrades and sends it.
let response = Client::default()
    .get("wss://echo.websocket.org/")
    .upgrade() // Prepares the WebSocket upgrade.
    .send()
    .await?;

// Turns the response into a WebSocket stream.
let mut websocket = response.into_websocket().await?;

// The WebSocket implements `Sink<Message>`.
websocket.send(Message::Text("Hello, World".into())).await?;

// The WebSocket is also a `TryStream` over `Message`s.
while let Some(message) = websocket.try_next().await? {
    if let Message::Text(text) = message {
        println!("received: {text}")
    }
}

Support for WebAssembly

reqwest-websocket uses the HTTP upgrade functionality built into reqwest, which is not available on WebAssembly. When you use reqwest-websocket in WebAssembly, it falls back to using web_sys::WebSocket. This means that everything except the URL (including query parameters) is not used for your request.

Dependencies

~5–21MB
~224K SLoC