[BREAKGLASS] Append-only mirror of github.com/signalapp/tungstenite-rs
Go to file
Alex Bakon a1837ccfe9
Some checks failed
CI / Format (push) Has been cancelled
CI / Clippy (push) Has been cancelled
CI / Test (stable) (push) Has been cancelled
CI / Test MSRV (1.63.0) (push) Has been cancelled
CI / Autobahn tests (beta) (push) Has been cancelled
CI / Autobahn tests (nightly) (push) Has been cancelled
CI / Autobahn tests (stable) (push) Has been cancelled
Merge latest upstream/master
Merge c0a099e3d1 from upstream to make
importing future changes easier.
2025-09-22 15:40:19 -04:00
.github rm dev-dependency env_logger for msrv check 2024-12-15 12:09:16 +01:00
autobahn Merge permessage-deflate support into master 2025-09-12 13:44:39 -04:00
benches Add end to end "send+recv" benchmarks (#497) 2025-05-24 00:43:53 +02:00
examples Merge permessage-deflate support into master 2025-09-12 13:44:39 -04:00
fuzz Rename methods to read, send, write & flush 2023-05-27 13:19:13 +01:00
scripts Enable autobahn testing with deflate support 2025-09-12 13:32:36 -04:00
src Merge latest upstream/master 2025-09-22 15:40:19 -04:00
tests Fix clippy warnings: Inline format! args 2025-07-09 14:10:39 +02:00
.gitignore Enable autobahn testing with deflate support 2025-09-12 13:32:36 -04:00
Cargo.toml Merge permessage-deflate support into master 2025-09-12 13:44:39 -04:00
CHANGELOG.md Reduce Error size 136 -> 32 (#511) 2025-07-24 12:50:33 +02:00
LICENSE-APACHE Relicense under MIT + Apache-2.0. 2017-04-11 16:18:09 +02:00
LICENSE-MIT Relicense under MIT + Apache-2.0. 2017-04-11 16:18:09 +02:00
README.md Perform extension negotiation during handshaking 2025-09-12 13:32:36 -04:00
rustfmt.toml Fix clippy warnings and deprecated settings 2021-07-21 10:43:44 +09:00

Tungstenite

Lightweight stream-based WebSocket implementation for Rust.

use std::net::TcpListener;
use std::thread::spawn;
use tungstenite::accept;

/// A WebSocket echo server
fn main () {
    let server = TcpListener::bind("127.0.0.1:9001").unwrap();
    for stream in server.incoming() {
        spawn (move || {
            let mut websocket = accept(stream.unwrap()).unwrap();
            loop {
                let msg = websocket.read().unwrap();

                // We do not want to send back ping/pong messages.
                if msg.is_binary() || msg.is_text() {
                    websocket.send(msg).unwrap();
                }
            }
        });
    }
}

Take a look at the examples section to see how to write a simple client/server.

NOTE: tungstenite-rs is more like a barebone to build reliable modern networking applications using WebSockets. If you're looking for a modern production-ready "batteries included" WebSocket library that allows you to efficiently use non-blocking sockets and do "full-duplex" communication, take a look at tokio-tungstenite.

MIT licensed Apache-2.0 licensed Crates.io Build Status

Documentation

Introduction

This library provides an implementation of WebSockets, RFC6455. It allows for both synchronous (like TcpStream) and asynchronous usage and is easy to integrate into any third-party event loops including MIO. The API design abstracts away all the internals of the WebSocket protocol but still makes them accessible for those who wants full control over the network.

Why Tungstenite?

It's formerly WS2, the 2nd implementation of WS. WS2 is the chemical formula of tungsten disulfide, the tungstenite mineral.

Features

Tungstenite provides a complete implementation of the WebSocket specification. TLS is supported on all platforms using native-tls or rustls. The following features are available:

  • native-tls
  • native-tls-vendored
  • rustls-tls-native-roots
  • rustls-tls-webpki-roots

Choose the one that is appropriate for your needs.

By default no TLS feature is activated, so make sure you use one of the TLS features, otherwise you won't be able to communicate with the TLS endpoints.

Testing

Tungstenite is thoroughly tested and passes the Autobahn Test Suite for WebSockets. It is also covered by internal unit tests as well as possible.

Benchmark

Benches are in ./benches.

  • Run all with cargo bench --bench \* -- --quick --noplot
  • Run a particular set with, say "e2e", with cargo bench --bench e2e -- --quick --noplot

Contributing

Please report bugs and make feature requests here.