Crates.io | tokio-websockets |
lib.rs | tokio-websockets |
version | 0.12.1 |
created_at | 2022-12-06 16:25:05.70085+00 |
updated_at | 2025-08-21 07:14:08.59165+00 |
description | High performance, strict, tokio-util based WebSockets implementation |
homepage | |
repository | https://github.com/Gelbpunkt/tokio-websockets/ |
max_upload_size | |
id | 731256 |
size | 602,243 |
High performance, strict, tokio-util based WebSockets implementation.
tokio
, tokio-util
, bytes
, futures-core
, futures-sink
, simdutf8
sha1_smol
(see Feature flags)Uri
from http
in the clientBytes
as payload storageFeature flags in tokio-websockets are added to allow tailoring it to your needs.
nightly
feature when using a nightly compiler will enable SIMD accelerated masking and UTF-8 validation on additional targets (see SIMD)client
enables a tiny client implementationserver
enables a tiny server implementationTLS is supported via any of the following feature flags:
native-tls
for a tokio-native-tls
backed implementationrustls-webpki-roots
for a tokio-rustls
backed implementation with webpki-roots
rustls-native-roots
for a tokio-rustls
backed implementation with rustls-native-certs
rustls-platform-verifier
for a tokio-rustls
backed implementation with rustls-platform-verifier
rustls-bring-your-own-connector
for a tokio-rustls
backed implementation that requires you to create your own Connector::Rustls
- the Connector::new
method will return a plain connectorThe rustls-*-roots
and rustls-platform-verifier
features require installing a crypto provider for rustls
, see here for more information.
One SHA1 implementation is required, usually provided by the TLS implementation:
ring
or aws_lc_rs
are used if the ring
or aws_lc_rs
features are enabled (recommended when rustls
is used)openssl
feature will use openssl
, usually preferred on most Linux/BSD systems with native-tls
sha1_smol
feature can be used as a fallback if no TLS is neededThe client
feature requires enabling one random number generator:
fastrand
can be used as a PRNG
getrandom
can be used as a cryptographically secure RNGrand
can be used as an alternative to fastrand
and should be preferred if it is already in the dependency treetokio-websockets makes use of SIMD to accelerate (un-)masking of messages and UTF-8 validation.
Architecture | Instructions | (Un-)masking | UTF-8 validation |
---|---|---|---|
aarch64 | NEON | ✅ | ✅ |
arm | NEON | ✅ (on nightly) | ✅ (on nightly) |
loongarch64 | LASX | ✅ (on nightly) | ❌ |
powerpc | AltiVec | ✅ (on nightly) | ❌ |
powerpc64 | AltiVec | ✅ (on nightly) | ❌ |
powerpc64le | AltiVec | ✅ (on nightly) | ❌ |
s390x | z13 vectors | ✅ (on nightly) | ❌ |
x86_64 | SSE2 | ✅ | ❌ |
x86_64 | AVX2 | ✅ | ✅ |
x86_64 | AVX512 | ✅ (on nightly) | ❌ |
This is a simple WebSocket echo server without any proper error handling.
More examples can be found in the examples folder.
use futures_util::{SinkExt, StreamExt};
use http::Uri;
use tokio::net::TcpListener;
use tokio_websockets::{ClientBuilder, Error, Message, ServerBuilder};
#[tokio::main]
async fn main() -> Result<(), Error> {
let listener = TcpListener::bind("127.0.0.1:3000").await?;
tokio::spawn(async move {
while let Ok((stream, _)) = listener.accept().await {
let (_request, mut ws_stream) = ServerBuilder::new()
.accept(stream)
.await?;
tokio::spawn(async move {
// Just an echo server, really
while let Some(Ok(msg)) = ws_stream.next().await {
if msg.is_text() || msg.is_binary() {
ws_stream.send(msg).await?;
}
}
Ok::<_, Error>(())
});
}
Ok::<_, Error>(())
});
let uri = Uri::from_static("ws://127.0.0.1:3000");
let (mut client, _) = ClientBuilder::from_uri(uri).connect().await?;
client.send(Message::text("Hello world!")).await?;
while let Some(Ok(msg)) = client.next().await {
if let Some(text) = msg.as_text() {
assert_eq!(text, "Hello world!");
// We got one message, just stop now
client.close().await?;
}
}
Ok(())
}
The current MSRV for all feature combinations is Rust 1.79.
WebSocket compression is currently unsupported.