| Crates.io | wstd-tungstenite |
| lib.rs | wstd-tungstenite |
| version | 0.2.2 |
| created_at | 2025-12-07 15:54:07.961707+00 |
| updated_at | 2025-12-09 23:46:18.563163+00 |
| description | WebSocket support for wstd, powered by tungstenite |
| homepage | https://github.com/jiftechnify/wstd-tungstenite |
| repository | https://github.com/jiftechnify/wstd-tungstenite |
| max_upload_size | |
| id | 1971841 |
| size | 46,401 |
Async WebSocket support for Wasm / WASI 0.2 applications on top of wstd.
cargo add wstd-tungstenite
# Provides extension methods for Stream/Sink
# You may want to add this for ergonomic WebSocketStream operations!
cargo add futures
Minimal WebSocket echo server:
// src/main.rs
use futures::{SinkExt, StreamExt};
use wstd::{io, iter::AsyncIterator, net::TcpListener};
use wstd_tungstenite::{accept_async, tungstenite::Message};
#[wstd::main]
async fn main() -> io::Result<()> {
let listener = TcpListener::bind("127.0.0.1:8080").await?;
println!("Listening on {}", listener.local_addr()?);
let mut incoming = listener.incoming();
while let Some(stream) = incoming.next().await {
let stream = stream?;
println!("Connection from {}", stream.peer_addr()?);
let mut ws = accept_async(stream)
.await
.expect("Error during the websocket handshake occurred");
wstd::runtime::spawn(async move {
while let Some(msg) = ws.next().await {
if let Ok(msg) = msg {
println!("received: {msg}");
if msg.is_text() && ws.send(msg).await.is_err() {
break;
}
} else {
break;
}
}
})
.detach();
}
Ok(())
}
Build it, then run on wasmtime:
cargo build --target wasm32-wasip2
wasmtime run -S inherit-network=y ./target/wasm32-wasip2/release/example.wasm
This crate is heavily inspired by tokio-tungstenite and async-tungstenite crates. Indeed, API is mostly same and much of the code is borrowed from them.
And, of course, big thanks to original tungstenite works!
MIT