1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//! # websockets-monoio
//!
//! A high-performance WebSocket client library built for the [`monoio`] async runtime
//! using `io_uring` on Linux. This library provides both `ws://` and `wss://` (TLS)
//! support with optimized low-allocation operations and efficient memory usage.
//!
//! ## Features
//!
//! - **🚀 High Performance**: Built on `monoio` runtime with `io_uring` for maximum efficiency on Linux
//! - **🔒 TLS Support**: Full `wss://` support via `monoio-rustls`
//! - **📦 Built for monoio**: Optimized for monoio async runtime
//! - **🛡️ Secure**: Uses `rustls` with `webpki-roots` for certificate validation
//! - **⚡ Low-Allocation**: Zero-copy message sending and optimized connection setup
//! - **🔧 Simple API**: Easy-to-use client interface
//!
//! ## Quick Start
//!
//! Add to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! websockets-monoio = "0.1"
//! monoio = "0.2"
//! fastwebsockets-monoio = "0.10"
//! anyhow = "1.0"
//! ```
//!
//! ## Basic Example
//!
//! ```no_run
//! use fastwebsockets_monoio::{Frame, OpCode};
//! use websockets_monoio::WsClient;
//!
//! #[monoio::main]
//! async fn main() -> anyhow::Result<()> {
//! // Connect to a WebSocket server
//! let mut client = WsClient::connect(
//! "wss://echo.websocket.org/",
//! &[],
//! ).await?;
//!
//! // Send a text message
//! client
//! .ws
//! .write_frame(Frame::text("Hello, WebSocket!".as_bytes().into()))
//! .await?;
//!
//! // Read the response
//! let frame = client.ws.read_frame().await?;
//! match frame.opcode {
//! OpCode::Text => {
//! let text = std::str::from_utf8(&frame.payload)?;
//! println!("Received: {}", text);
//! }
//! OpCode::Close => {
//! println!("Connection closed by server");
//! }
//! _ => {}
//! }
//!
//! Ok(())
//! }
//! ```
//!
//! ## Cryptocurrency Exchange Example
//!
//! ```no_run
//! use fastwebsockets_monoio::{Frame, OpCode};
//! use websockets_monoio::WsClient;
//!
//! #[monoio::main]
//! async fn main() -> anyhow::Result<()> {
//! // Connect to Binance ticker stream
//! let mut client = WsClient::connect(
//! "wss://stream.binance.com:9443/ws/btcusdt@trade",
//! &[],
//! ).await?;
//!
//! // Subscribe to trades
//! let subscribe = r#"{"method":"SUBSCRIBE","params":["btcusdt@trade"],"id":1}"#;
//! client
//! .ws
//! .write_frame(Frame::text(subscribe.as_bytes().into()))
//! .await?;
//!
//! // Stream trade data
//! loop {
//! let frame = client.ws.read_frame().await?;
//! match frame.opcode {
//! OpCode::Text => {
//! let text = std::str::from_utf8(&frame.payload)?;
//! println!("Trade: {}", text);
//! }
//! OpCode::Binary => {
//! println!("Binary frame ({} bytes)", frame.payload.len());
//! }
//! OpCode::Close => {
//! println!("Stream closed");
//! break;
//! }
//! OpCode::Ping | OpCode::Pong => {
//! // Auto-handled by fastwebsockets
//! }
//! _ => {}
//! }
//! }
//!
//! Ok(())
//! }
//! ```
//!
//! ## Platform Support
//!
//! - **Linux**: Full support with `io_uring` (recommended)
//! - **macOS/Windows**: Limited support (falls back to standard async I/O)
//!
//! For maximum performance, deploy on Linux with kernel version 5.1+ for full `io_uring` support.
//!
//! [`monoio`]: https://docs.rs/monoio
pub use ;