Crates.io | reqwest-websocket |
lib.rs | reqwest-websocket |
version | |
source | src |
created_at | 2024-02-14 00:06:07.799534+00 |
updated_at | 2025-05-19 04:38:37.568079+00 |
description | WebSocket connections with reqwest |
homepage | https://github.com/jgraef/reqwest-websocket |
repository | https://github.com/jgraef/reqwest-websocket |
max_upload_size | |
id | 1139084 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
reqwest-websocket
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.
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}")
}
}
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.