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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//! WebSocket client for real-time Kalshi market data and trading events.
//!
//! This module provides a WebSocket connection to the Kalshi exchange for receiving
//! real-time updates on orderbooks, trades, fills, positions, and more. The WebSocket
//! API enables low-latency market data streaming and live trading notifications.
//!
//! # Overview
//!
//! The WebSocket API is built on authenticated persistent connections that stream
//! real-time data updates. This is significantly more efficient than polling REST
//! endpoints when you need continuous market data or order updates.
//!
//! ## Key Features
//!
//! - **Real-time market data**: Live orderbook updates, trades, and ticker changes
//! - **Portfolio updates**: Instant notifications of fills, order updates, and position changes
//! - **Multiple channel subscriptions**: Subscribe to multiple markets and data types simultaneously
//! - **Automatic authentication**: RSA-PSS signing handled automatically during connection
//! - **Asynchronous stream interface**: Integrates with Tokio and futures for efficient async programming
//!
//! # Quick Start
//!
//! ```rust,ignore
//! use kalshi::{Kalshi, TradingEnvironment};
//! use futures_util::StreamExt;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! // Create HTTP client first
//! let kalshi = Kalshi::new(
//! TradingEnvironment::DemoMode,
//! "your-key-id",
//! "path/to/private.pem"
//! ).await?;
//!
//! // Create WebSocket client from HTTP client
//! let mut ws = kalshi.websocket();
//!
//! // Connect to WebSocket server
//! ws.connect().await?;
//!
//! // Subscribe to orderbook updates for a market
//! ws.subscribe_to_orderbook_delta("HIGHNY-24JAN15-T50").await?;
//!
//! // Process incoming messages
//! let mut stream = ws.messages();
//! while let Some(msg) = stream.next().await {
//! match msg {
//! WebSocketMessage::OrderbookDelta(delta) => {
//! println!("Orderbook updated: {:?}", delta);
//! }
//! WebSocketMessage::Trade(trade) => {
//! println!("Trade executed: {:?}", trade);
//! }
//! _ => {}
//! }
//! }
//! # Ok(())
//! # }
//! ```
//!
//! # Available Channels
//!
//! The WebSocket API supports several subscription channels:
//!
//! ## Market Data Channels
//!
//! - **Orderbook Delta** - Real-time orderbook updates (incremental changes)
//! - **Orderbook Snapshot** - Full orderbook snapshots at regular intervals
//! - **Ticker** - Market ticker updates (best bid/ask, last price, volume)
//! - **Trade** - Individual trade executions
//! - **Trades** - Trade feed for specific markets
//!
//! ## Portfolio Channels (Authenticated)
//!
//! - **Fill** - Your order fills as they occur
//! - **Order** - Your order status updates (created, canceled, filled)
//!
//! # Message Types
//!
//! All messages received from the WebSocket conform to the [`WebSocketMessage`] enum,
//! which includes:
//!
//! - Control messages: `Subscribed`, `Ok`, `Error`, `Heartbeat`
//! - Market data: `OrderbookDelta`, `OrderbookSnapshot`, `Ticker`, `Trade`, `Trades`
//! - Portfolio updates: `Fill`, `Order`
//!
//! # Connection Lifecycle
//!
//! 1. **Create** - Initialize the client with credentials
//! 2. **Connect** - Establish WebSocket connection with authentication
//! 3. **Subscribe** - Subscribe to desired channels
//! 4. **Stream** - Receive and process messages via the async stream
//! 5. **Unsubscribe** (optional) - Remove subscriptions dynamically
//! 6. **Disconnect** - Close the connection gracefully
//!
//! # Error Handling
//!
//! WebSocket operations return [`KalshiError`](crate::KalshiError) for:
//! - Connection failures
//! - Authentication errors
//! - Subscription errors (invalid ticker, channel not available, etc.)
//! - Network timeouts
//!
//! # Examples
//!
//! ## Subscribe to Multiple Markets
//!
//! ```rust,ignore
//! # use kalshi::Kalshi;
//! # async fn example(ws: &mut kalshi::KalshiWebSocket) -> Result<(), Box<dyn std::error::Error>> {
//! // Subscribe to ticker updates for multiple markets
//! ws.subscribe_to_ticker("HIGHNY-24JAN15-T50").await?;
//! ws.subscribe_to_ticker("INXD-24FEB01").await?;
//! ws.subscribe_to_ticker("NASDAQ-24MAR15").await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Monitor Your Fills
//!
//! ```rust,ignore
//! # use kalshi::{Kalshi, WebSocketMessage};
//! # use futures_util::StreamExt;
//! # async fn example(ws: &mut kalshi::KalshiWebSocket) -> Result<(), Box<dyn std::error::Error>> {
//! // Subscribe to your fill notifications
//! ws.subscribe_to_fills().await?;
//!
//! let mut stream = ws.messages();
//! while let Some(msg) = stream.next().await {
//! if let WebSocketMessage::Fill(fill) = msg {
//! println!("Fill received!");
//! println!(" Ticker: {}", fill.ticker);
//! println!(" Side: {:?}", fill.side);
//! println!(" Count: {}", fill.count);
//! println!(" Price: {}", fill.yes_price);
//! }
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ## Track Orderbook Changes
//!
//! ```rust,ignore
//! # use kalshi::{Kalshi, WebSocketMessage};
//! # use futures_util::StreamExt;
//! # async fn example(ws: &mut kalshi::KalshiWebSocket) -> Result<(), Box<dyn std::error::Error>> {
//! ws.subscribe_to_orderbook_delta("HIGHNY-24JAN15-T50").await?;
//!
//! let mut stream = ws.messages();
//! while let Some(msg) = stream.next().await {
//! if let WebSocketMessage::OrderbookDelta(delta) = msg {
//! println!("Market: {}", delta.ticker);
//! if let Some(yes_levels) = delta.yes {
//! println!("Yes side updates: {} levels", yes_levels.len());
//! }
//! if let Some(no_levels) = delta.no {
//! println!("No side updates: {} levels", no_levels.len());
//! }
//! }
//! }
//! # Ok(())
//! # }
//! ```
//!
//! # Performance Considerations
//!
//! - **Selective subscriptions**: Only subscribe to the channels and markets you need
//! - **Message filtering**: Filter messages in your application to process only what's needed
//! - **Orderbook deltas vs snapshots**: Use deltas for efficiency, snapshots for periodic synchronization
//! - **Connection management**: Reuse a single connection for multiple subscriptions
//!
//! # See Also
//!
//! - [`KalshiWebSocket`](connection::KalshiWebSocket) - The main WebSocket client
//! - [`WebSocketMessage`](messages::WebSocketMessage) - All message types
//! - [`Channel`](channels::Channel) - Available subscription channels
//! - [`Subscription`](subscription::Subscription) - Subscription management
pub use Channel;
pub use ;
pub use *;
pub use ;