WebSocket
Implementation of RFC6455 and RFC7692. WebSocket is a communication protocol that enables full-duplex communication between a client (typically a web browser) and a server over a single TCP connection. Unlike traditional HTTP, which is request-response based, WebSocket allows real-time data exchange without the need for polling.
In-house benchmarks are available at https://c410-f3r.github.io/wtx-bench. If you are aware of other benchmark tools, please open a discussion in the GitHub project.
To use this functionality, it is necessary to activate the web-socket
feature.
Autobahn Reports
Compression
The "permessage-deflate" extension is the only supported compression format and is backed by the zlib-rs
project that performs as well as zlib-ng
.
To get the most performance possible, try compiling your program with RUSTFLAGS='-C target-cpu=native'
to allow zlib-rs
to use more efficient SIMD instructions.
No masking
Although not officially endorsed, the no-masking
parameter described at https://datatracker.ietf.org/doc/html/draft-damjanovic-websockets-nomasking-02 is supported to increase performance. If such a thing is not desirable, please make sure to check the handshake parameters to avoid accidental scenarios.
To make everything work as intended both parties, client and server, need to implement this feature. For example, web browser won't stop masking frames.
Client Example
//! WebSocket CLI client that enables real-time communication by allowing users to send and //! receive messages through typing. //! //! This snippet requires ~35 dependencies and has an optimized binary size of ~550K. extern crate tokio; extern crate wtx; extern crate wtx_instances; use tokio::{ io::{AsyncBufReadExt, BufReader}, net::TcpStream, }; use wtx::{ misc::{simple_seed, Uri, Xorshift64}, web_socket::{Frame, OpCode, WebSocket, WebSocketBuffer}, }; #[tokio::main] async fn main() -> wtx::Result<()> { let uri = Uri::new("ws://www.example.com"); let mut ws = WebSocket::connect( (), [], false, Xorshift64::from(simple_seed()), TcpStream::connect(uri.hostname_with_implied_port()).await?, &uri.to_ref(), WebSocketBuffer::default(), |_| wtx::Result::Ok(()), ) .await?; let mut buffer = Vec::new(); let mut reader = BufReader::new(tokio::io::stdin()); loop { tokio::select! { frame_rslt = ws.read_frame() => { let frame = frame_rslt?; match (frame.op_code(), frame.text_payload()) { (_, Some(elem)) => println!("{elem}"), (OpCode::Close, _) => break, _ => {} } } read_rslt = reader.read_until(b'\n', &mut buffer) => { let _ = read_rslt?; ws.write_frame(&mut Frame::new_fin(OpCode::Text, &mut buffer)).await?; } } } Ok(()) }
Server Example
//! Serves requests using low-level WebSockets resources along side self-made certificates. extern crate tokio; extern crate tokio_rustls; extern crate wtx; extern crate wtx_instances; use tokio::net::TcpStream; use tokio_rustls::server::TlsStream; use wtx::{ http::OptionedServer, misc::TokioRustlsAcceptor, web_socket::{OpCode, WebSocket, WebSocketBuffer}, }; #[tokio::main] async fn main() -> wtx::Result<()> { OptionedServer::web_socket_tokio( &wtx_instances::host_from_args(), None, || {}, |error| eprintln!("{error}"), handle, ( || { TokioRustlsAcceptor::without_client_auth() .build_with_cert_chain_and_priv_key(wtx_instances::CERT, wtx_instances::KEY) }, |acceptor| acceptor.clone(), |acceptor, stream| async move { Ok(acceptor.accept(stream).await?) }, ), ) .await } async fn handle( mut ws: WebSocket<(), TlsStream<TcpStream>, &mut WebSocketBuffer, false>, ) -> wtx::Result<()> { let (mut common, mut reader, mut writer) = ws.parts_mut(); loop { let mut frame = reader.read_frame(&mut common).await?; match frame.op_code() { OpCode::Binary | OpCode::Text => { writer.write_frame(&mut common, &mut frame).await?; } OpCode::Close => break, _ => {} } } Ok(()) }