wstd-tungstenite

Crates.iowstd-tungstenite
lib.rswstd-tungstenite
version0.2.2
created_at2025-12-07 15:54:07.961707+00
updated_at2025-12-09 23:46:18.563163+00
descriptionWebSocket support for wstd, powered by tungstenite
homepagehttps://github.com/jiftechnify/wstd-tungstenite
repositoryhttps://github.com/jiftechnify/wstd-tungstenite
max_upload_size
id1971841
size46,401
Takumi Fujiwara (jiftechnify)

documentation

README

wstd-tungstenite

Async WebSocket support for Wasm / WASI 0.2 applications on top of wstd.

Crates.io Docs.rs MIT Licensed

Installation

cargo add wstd-tungstenite

# Provides extension methods for Stream/Sink
# You may want to add this for ergonomic WebSocketStream operations!
cargo add futures

Example

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

Credits

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!

License

MIT

Commit count: 0

cargo fmt