42 releases (22 breaking)

0.24.0 Aug 27, 2024
0.23.0 Jun 2, 2024
0.22.0 Jun 1, 2024
0.21.0 Dec 7, 2023
0.1.1 Mar 22, 2017

#1 in WebSocket

Download history 816435/week @ 2024-08-23 772317/week @ 2024-08-30 852543/week @ 2024-09-06 730798/week @ 2024-09-13 1077370/week @ 2024-09-20 964508/week @ 2024-09-27 1401243/week @ 2024-10-04 1303931/week @ 2024-10-11 1407725/week @ 2024-10-18 934642/week @ 2024-10-25 867939/week @ 2024-11-01 879245/week @ 2024-11-08 952581/week @ 2024-11-15 756154/week @ 2024-11-22 797875/week @ 2024-11-29 918417/week @ 2024-12-06

3,622,160 downloads per month
Used in 2,749 crates (298 directly)

MIT/Apache

175KB
3.5K SLoC

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.

There is no support for permessage-deflate at the moment, but the PRs are welcome 😉

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.

Contributing

Please report bugs and make feature requests here.

Dependencies

~0.8–12MB
~173K SLoC