1# Tungstenite 2 3Lightweight stream-based WebSocket implementation for [Rust](https://www.rust-lang.org/). 4 5```rust 6use std::net::TcpListener; 7use std::thread::spawn; 8use tungstenite::accept; 9 10/// A WebSocket echo server 11fn main () { 12 let server = TcpListener::bind("127.0.0.1:9001").unwrap(); 13 for stream in server.incoming() { 14 spawn (move || { 15 let mut websocket = accept(stream.unwrap()).unwrap(); 16 loop { 17 let msg = websocket.read().unwrap(); 18 19 // We do not want to send back ping/pong messages. 20 if msg.is_binary() || msg.is_text() { 21 websocket.send(msg).unwrap(); 22 } 23 } 24 }); 25 } 26} 27``` 28 29Take a look at the examples section to see how to write a simple client/server. 30 31**NOTE:** `tungstenite-rs` is more like a barebone to build reliable modern networking applications 32using WebSockets. If you're looking for a modern production-ready "batteries included" WebSocket 33library that allows you to efficiently use non-blocking sockets and do "full-duplex" communication, 34take a look at [`tokio-tungstenite`](https://github.com/snapview/tokio-tungstenite). 35 36[](./LICENSE-MIT) 37[](./LICENSE-APACHE) 38[](https://crates.io/crates/tungstenite) 39[](https://github.com/snapview/tungstenite-rs/actions) 40 41[Documentation](https://docs.rs/tungstenite) 42 43Introduction 44------------ 45This library provides an implementation of WebSockets, 46[RFC6455](https://tools.ietf.org/html/rfc6455). It allows for both synchronous (like TcpStream) 47and asynchronous usage and is easy to integrate into any third-party event loops including 48[MIO](https://github.com/tokio-rs/mio). The API design abstracts away all the internals of the 49WebSocket protocol but still makes them accessible for those who wants full control over the 50network. 51 52Why Tungstenite? 53---------------- 54 55It's formerly WS2, the 2nd implementation of WS. WS2 is the chemical formula of 56tungsten disulfide, the tungstenite mineral. 57 58Features 59-------- 60 61Tungstenite provides a complete implementation of the WebSocket specification. 62TLS is supported on all platforms using `native-tls` or `rustls`. The following 63features are available: 64 65* `native-tls` 66* `native-tls-vendored` 67* `rustls-tls-native-roots` 68* `rustls-tls-webpki-roots` 69 70Choose the one that is appropriate for your needs. 71 72By default **no TLS feature is activated**, so make sure you use one of the TLS features, 73otherwise you won't be able to communicate with the TLS endpoints. 74 75There is no support for permessage-deflate at the moment, but the PRs are welcome :wink: 76 77Testing 78------- 79 80Tungstenite is thoroughly tested and passes the [Autobahn Test Suite](https://github.com/crossbario/autobahn-testsuite) for 81WebSockets. It is also covered by internal unit tests as well as possible. 82 83Contributing 84------------ 85 86Please report bugs and make feature requests [here](https://github.com/snapview/tungstenite-rs/issues). 87