kiteticker_async_manager/lib.rs
1#![allow(
2 clippy::cognitive_complexity,
3 clippy::large_enum_variant,
4 clippy::needless_doctest_main
5)]
6#![warn(missing_debug_implementations, rust_2018_idioms, unreachable_pub)]
7#![doc(test(
8 no_crate_inject,
9 attr(deny(warnings, rust_2018_idioms), allow(dead_code, unused_variables))
10))]
11
12//! # KiteTicker Async Manager
13//!
14//! High-performance async WebSocket client for the [Kite Connect API](https://kite.trade/docs/connect/v3/websocket/#websocket-streaming)
15//! with multi-connection support and dynamic subscription management.
16//!
17//! ## Features
18//!
19//! - **๐ Multi-Connection Support** - Utilize all 3 allowed WebSocket connections (9,000 symbol capacity)
20//! - **โก High Performance** - Dedicated parser tasks, optimized buffers, sub-microsecond latency
21//! - **๐ Dynamic Subscriptions** - Add/remove symbols at runtime without reconnection
22//! - **๐ Load Balancing** - Automatic symbol distribution across connections
23//! - **๐ช Production Ready** - Comprehensive error handling, health monitoring, reconnection
24//! - **๐ง Async-First Design** - Built with Tokio, follows Rust async best practices
25//!
26//! ## Quick Start
27//!
28//! ### Multi-Connection Manager (Recommended)
29//!
30//! ```rust,no_run
31//! use kiteticker_async_manager::{KiteTickerManager, KiteManagerConfig, Mode, TickerMessage};
32//!
33//! #[tokio::main]
34//! async fn main() -> Result<(), String> {
35//! // Setup credentials
36//! let api_key = std::env::var("KITE_API_KEY").unwrap();
37//! let access_token = std::env::var("KITE_ACCESS_TOKEN").unwrap();
38//!
39//! // Create high-performance manager
40//! let config = KiteManagerConfig {
41//! max_connections: 3,
42//! max_symbols_per_connection: 3000,
43//! enable_dedicated_parsers: true,
44//! default_mode: Mode::LTP,
45//! ..Default::default()
46//! };
47//!
48//! // Start manager
49//! let mut manager = KiteTickerManager::new(api_key, access_token, config);
50//! manager.start().await?;
51//!
52//! // Subscribe to symbols (automatically distributed)
53//! let symbols = vec![256265, 408065, 738561]; // NIFTY 50, HDFC Bank, Reliance
54//! manager.subscribe_symbols(&symbols, Some(Mode::Quote)).await?;
55//!
56//! // Process data from independent channels
57//! let channels = manager.get_all_channels();
58//! for (channel_id, mut receiver) in channels {
59//! tokio::spawn(async move {
60//! while let Ok(message) = receiver.recv().await {
61//! if let TickerMessage::Ticks(ticks) = message {
62//! for tick in ticks {
63//! println!("Channel {:?}: {} @ โน{:.2}",
64//! channel_id,
65//! tick.instrument_token,
66//! tick.content.last_price.unwrap_or(0.0));
67//! }
68//! }
69//! }
70//! });
71//! }
72//!
73//! // Dynamic operations
74//! manager.subscribe_symbols(&[5633, 884737], Some(Mode::Full)).await?; // Add
75//! manager.unsubscribe_symbols(&[408065]).await?; // Remove
76//! manager.change_mode(&[256265], Mode::Full).await?; // Change mode
77//!
78//! Ok(())
79//! }
80//! ```
81//!
82//!
83//! #[tokio::main]
84//! async fn main() -> Result<(), String> {
85//! let api_key = std::env::var("KITE_API_KEY").unwrap();
86//! let access_token = std::env::var("KITE_ACCESS_TOKEN").unwrap();
87//!
88//! // Connect to WebSocket
89//! let ticker = KiteTickerAsync::connect(&api_key, &access_token).await?;
90//!
91//! // Subscribe to symbols
92//! let symbols = vec![256265, 408065]; // NIFTY 50, HDFC Bank
93//! let mut subscriber = ticker.subscribe(&symbols, Some(Mode::LTP)).await?;
94//!
95//! // Receive data
96//! while let Ok(Some(message)) = subscriber.next_message().await {
97//! if let TickerMessage::Ticks(ticks) = message {
98//! for tick in ticks {
99//! println!("Symbol {}: โน{:.2}",
100//! tick.instrument_token,
101//! tick.content.last_price.unwrap_or(0.0));
102//! }
103//! }
104//! }
105//!
106//! Ok(())
107//! }
108//! ```
109//!
110//! ## Performance Comparison
111//!
112//! | Feature | Single Connection | Multi-Connection Manager | Improvement |
113//! |---------|------------------|---------------------------|-------------|
114//! | **Max Symbols** | 3,000 | 9,000 | **3x capacity** |
115//! | **Throughput** | Limited by 1 connection | 3 parallel connections | **3x throughput** |
116//! | **Latency** | ~5-10ยตs | ~1-2ยตs | **5x faster** |
117//! | **Resilience** | Single point of failure | 3 independent connections | **High availability** |
118//! | **Dynamic Ops** | Manual reconnection | Runtime add/remove | **Zero downtime** |
119//!
120//! ## Architecture
121//!
122//! The library provides two main components:
123//!
124//! ### 1. [`KiteTickerAsync`] - Single WebSocket Connection
125//! - Direct WebSocket client for simple use cases
126//! - Up to 3,000 symbols per connection
127//! - Manual connection management
128//!
129//! ### 2. [`KiteTickerManager`] - Multi-Connection Manager (Recommended)
130//! - Manages up to 3 WebSocket connections automatically
131//! - Supports up to 9,000 symbols total
132//! - Dynamic subscription management
133//! - Load balancing and health monitoring
134//! - High-performance optimizations
135//!
136//! ## Subscription Modes
137//!
138//! The library supports three subscription modes:
139//!
140//! - **[`Mode::LTP`]** - Last traded price only (minimal bandwidth)
141//! - **[`Mode::Quote`]** - Price + volume + OHLC (standard data)
142//! - **[`Mode::Full`]** - Complete market depth (maximum data)
143//!
144//! ## Examples
145//!
146//! See the [examples directory](https://github.com/kaychaks/kiteticker-async/tree/master/examples) for:
147//!
148//! - **Basic Examples** - Simple usage patterns
149//! - **Advanced Examples** - Complex multi-connection scenarios
150//! - **Performance Examples** - Optimization and benchmarking
151//!
152//! ## Documentation
153//!
154//! - [Getting Started Guide](https://github.com/kaychaks/kiteticker-async/blob/master/docs/guides/getting-started.md)
155//! - [API Reference](https://github.com/kaychaks/kiteticker-async/blob/master/docs/api/)
156//! - [Dynamic Subscriptions](https://github.com/kaychaks/kiteticker-async/blob/master/docs/guides/DYNAMIC_SUBSCRIPTION_GUIDE.md)
157//! - [Performance Guide](https://github.com/kaychaks/kiteticker-async/blob/master/docs/guides/PERFORMANCE_IMPROVEMENTS.md) {
158//! match msg {
159//! TickerMessage::Ticks(ticks) => {
160//! let tick = ticks.first().unwrap();
161//! println!("Received tick for instrument_token {}, {:?}", tick.instrument_token, tick);
162//! break;
163//! },
164//! _ => continue,
165//! }
166//! }
167//! }
168//!
169//! Ok(())
170//! }
171//! ```
172mod errors;
173mod models;
174mod parser;
175pub mod manager;
176pub use errors::ParseTickError;
177pub use models::{
178 Depth, DepthItem, Exchange, Mode, Order, OrderStatus, OrderTransactionType,
179 OrderValidity, Request, TextMessage, Tick, TickMessage, TickerMessage, OHLC,
180};
181
182pub mod ticker;
183pub use ticker::{KiteTickerAsync, KiteTickerSubscriber};
184pub use manager::{KiteTickerManager, KiteManagerConfig, ChannelId, ManagerStats, HealthSummary};