#websocket #io #web

tungstenite

Lightweight stream-based WebSocket implementation

27 releases

0.13.0 Feb 8, 2021
0.11.1 Aug 7, 2020
0.11.0 Jun 12, 2020
0.10.1 Feb 2, 2020
0.1.1 Mar 22, 2017

#1 in WebSocket

Download history 26210/week @ 2020-12-23 31318/week @ 2020-12-30 50346/week @ 2021-01-06 47821/week @ 2021-01-13 53921/week @ 2021-01-20 52179/week @ 2021-01-27 52540/week @ 2021-02-03 56818/week @ 2021-02-10 61787/week @ 2021-02-17 58905/week @ 2021-02-24 58132/week @ 2021-03-03 58818/week @ 2021-03-10 53923/week @ 2021-03-17 58492/week @ 2021-03-24 58861/week @ 2021-03-31 56339/week @ 2021-04-07

206,709 downloads per month
Used in 299 crates (80 directly)

MIT/Apache

145KB
3K SLoC

Tungstenite

Lightweight stream-based WebSocket implementation for Rust.

use std::net::TcpListener;
use std::thread::spawn;
use tungstenite::server::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_message().unwrap();

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

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

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 available through the native-tls and rustls-tls feature flags.

There is no support for permessage-deflate at the moment. It's planned.

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

~3.5–6.5MB
~159K SLoC