bybit_client/ws/mod.rs
1//! WebSocket client implementation.
2//!
3//! This module provides WebSocket support for:
4//! - Public streams (orderbook, trades, tickers, klines, liquidations).
5//! - Private streams (positions, orders, executions, wallet updates).
6//! - WebSocket Trade API for order management.
7//! - Local orderbook management with delta updates.
8//!
9//! # Public Streams Example
10//!
11//! ```no_run
12//! use bybit_client::ws::{WsClient, WsChannel, WsMessage};
13//!
14//! #[tokio::main]
15//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
16//! // Connect to linear perpetual public stream.
17//! let (client, mut receiver) = WsClient::connect_public(WsChannel::PublicLinear).await?;
18//!
19//! // Subscribe to orderbook and trades.
20//! client.subscribe(&["orderbook.50.BTCUSDT", "publicTrade.BTCUSDT"]).await?;
21//!
22//! // Process incoming messages.
23//! while let Some(msg) = receiver.recv().await {
24//! match msg {
25//! WsMessage::Orderbook(ob) => {
26//! println!("Orderbook update for {}: {} bids, {} asks",
27//! ob.data.symbol, ob.data.bids.len(), ob.data.asks.len());
28//! }
29//! WsMessage::Trade(trades) => {
30//! for trade in &trades.data {
31//! println!("Trade: {} {} @ {}",
32//! trade.symbol, trade.size, trade.price);
33//! }
34//! }
35//! _ => {}
36//! }
37//! }
38//!
39//! Ok(())
40//! }
41//! ```
42//!
43//! # Local Orderbook Example
44//!
45//! ```no_run
46//! use bybit_client::ws::{WsClient, WsChannel, WsMessage, LocalOrderbook};
47//!
48//! #[tokio::main]
49//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
50//! let (client, mut receiver) = WsClient::connect_public(WsChannel::PublicLinear).await?;
51//! client.subscribe(&["orderbook.50.BTCUSDT"]).await?;
52//!
53//! let mut orderbook = LocalOrderbook::new("BTCUSDT");
54//!
55//! while let Some(msg) = receiver.recv().await {
56//! if let WsMessage::Orderbook(update) = msg {
57//! orderbook.apply_update(&update)?;
58//!
59//! if let (Some(bid), Some(ask)) = (orderbook.best_bid(), orderbook.best_ask()) {
60//! println!("Best bid: {} @ {}, Best ask: {} @ {}",
61//! bid.size, bid.price, ask.size, ask.price);
62//! println!("Spread: {:.2}", orderbook.spread().unwrap_or(0.0));
63//! }
64//! }
65//! }
66//!
67//! Ok(())
68//! }
69//! ```
70//!
71//! # Private Streams Example
72//!
73//! ```no_run
74//! use bybit_client::ws::{WsClient, WsChannel};
75//!
76//! #[tokio::main]
77//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
78//! // Connect with authentication.
79//! let (client, mut receiver) = WsClient::connect_private(
80//! "your_api_key",
81//! "your_api_secret",
82//! ).await?;
83//!
84//! // Subscribe to private topics.
85//! client.subscribe(&["position", "order", "execution"]).await?;
86//!
87//! while let Some(msg) = receiver.recv().await {
88//! println!("Private message: {:?}", msg);
89//! }
90//!
91//! Ok(())
92//! }
93//! ```
94//!
95//! # WebSocket Topics
96//!
97//! ## Public Topics
98//! - `orderbook.{depth}.{symbol}` - Orderbook (depth: 1, 50, 200, 500).
99//! - `publicTrade.{symbol}` - Public trades.
100//! - `tickers.{symbol}` - Ticker updates.
101//! - `kline.{interval}.{symbol}` - Kline or candlestick data.
102//! - `liquidation.{symbol}` - Liquidation events.
103//!
104//! ## Private Topics
105//! - `position` - Position updates.
106//! - `execution` - Execution updates.
107//! - `order` - Order updates.
108//! - `wallet` - Wallet balance updates.
109//! - `greeks` - Options greeks updates.
110
111pub mod client;
112pub mod orderbook;
113pub mod stream;
114pub mod trade;
115pub mod types;
116
117pub use client::WsClient;
118pub use orderbook::{LocalOrderbook, PriceLevel};
119pub use stream::{IntoWsStream, WsStream};
120pub use trade::{
121 AmendOrderRequest, BatchOrderResult, CancelOrderRequest, CreateOrderRequest, OrderResult,
122 WsTradeClient, WsTradeResponse,
123};
124pub use types::*;