kiteticker-async-manager 0.4.0

High-performance async WebSocket client for Kite Connect API with multi-connection support, dynamic subscription management, and optimized data processing.
Documentation
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# Multi-API Manager Guide

The `MultiApiKiteTickerManager` enables managing multiple Kite Connect API credentials simultaneously within a single manager instance.

## Overview

### Key Features

- **Multiple API Keys**: Manage multiple Kite Connect accounts from a single manager
- **Per-API Connection Pools**: Each API key maintains up to 3 WebSocket connections
- **Flexible Distribution**: Round-robin or manual symbol assignment
- **Unified Message Stream**: All messages available through a single channel with API key identification
- **Aggregate Monitoring**: Track health and statistics across all API keys
- **Backward Compatible**: Works alongside existing single-API manager

### Use Cases

1. **Multi-Account Trading**: Monitor and trade across multiple trading accounts
2. **High-Volume Subscriptions**: Distribute 18,000+ symbols across multiple API keys
3. **Organizational Isolation**: Separate symbols by department or strategy
4. **Redundancy**: Use multiple API keys for failover scenarios

## Quick Start

### Basic Setup

```rust
use kiteticker_async_manager::{
    MultiApiKiteTickerManager,
    Mode,
    DistributionStrategy,
};

#[tokio::main]
async fn main() -> Result<(), String> {
    // Create manager with multiple API keys
    let mut manager = MultiApiKiteTickerManager::builder()
        .add_api_key("account1", "api_key_1", "access_token_1")
        .add_api_key("account2", "api_key_2", "access_token_2")
        .max_connections_per_api(3)
        .distribution_strategy(DistributionStrategy::RoundRobin)
        .build();

    // Start all connections
    manager.start().await?;

    // Subscribe symbols (auto-distributed)
    let symbols = vec![256265, 408065, 738561];
    manager.subscribe_symbols(&symbols, Some(Mode::Quote)).await?;

    // Receive messages from all API keys
    let mut unified_channel = manager.get_unified_channel();
    while let Ok((api_key_id, message)) = unified_channel.recv().await {
        println!("From {}: {:?}", api_key_id.0, message);
    }

    Ok(())
}
```

## Configuration

### Builder Pattern

The `MultiApiKiteTickerManagerBuilder` provides a fluent API for configuration:

```rust
let mut manager = MultiApiKiteTickerManager::builder()
    // Add API keys
    .add_api_key("primary", "api_key_1", "token_1")
    .add_api_key("secondary", "api_key_2", "token_2")
    
    // Connection settings
    .max_connections_per_api(3)
    .max_symbols_per_connection(3000)
    .connection_timeout(Duration::from_secs(30))
    
    // Distribution strategy
    .distribution_strategy(DistributionStrategy::RoundRobin)
    
    // Subscription defaults
    .default_mode(Mode::Quote)
    
    // Monitoring
    .enable_health_monitoring(true)
    .health_check_interval(Duration::from_secs(10))
    
    .build();
```

### Distribution Strategies

#### Round-Robin (Default)

Automatically distributes symbols evenly across all API keys:

```rust
let mut manager = MultiApiKiteTickerManager::builder()
    .add_api_key("account1", api_key_1, token_1)
    .add_api_key("account2", api_key_2, token_2)
    .distribution_strategy(DistributionStrategy::RoundRobin)
    .build();

manager.start().await?;

// Symbols automatically distributed across both API keys
manager.subscribe_symbols(&symbols, Some(Mode::Quote)).await?;
```

#### Manual Assignment

Explicitly assign symbols to specific API keys:

```rust
let mut manager = MultiApiKiteTickerManager::builder()
    .add_api_key("account1", api_key_1, token_1)
    .add_api_key("account2", api_key_2, token_2)
    .distribution_strategy(DistributionStrategy::Manual)
    .build();

manager.start().await?;

// Assign specific symbols to specific API keys
manager.subscribe_symbols_to_api("account1", &nifty_symbols, Some(Mode::Full)).await?;
manager.subscribe_symbols_to_api("account2", &stock_symbols, Some(Mode::LTP)).await?;
```

## Symbol Management

### Subscribe Symbols

#### Auto-Distribution (Round-Robin Only)

```rust
// Automatically distribute across all API keys
let symbols = vec![256265, 408065, 738561];
manager.subscribe_symbols(&symbols, Some(Mode::Quote)).await?;
```

#### Manual Assignment

```rust
// Assign to specific API key
manager.subscribe_symbols_to_api(
    "account1", 
    &symbols, 
    Some(Mode::Quote)
).await?;
```

### Unsubscribe Symbols

```rust
// Unsubscribe regardless of which API key owns them
manager.unsubscribe_symbols(&symbols).await?;
```

### Change Mode

```rust
// Change mode for symbols across all API keys
manager.change_mode(&symbols, Mode::Full).await?;
```

## Message Handling

### Unified Channel

Receive messages from all API keys through a single channel:

```rust
let mut unified_channel = manager.get_unified_channel();

while let Ok((api_key_id, message)) = unified_channel.recv().await {
    match message {
        TickerMessage::Ticks(ticks) => {
            println!("API Key {}: Received {} ticks", api_key_id.0, ticks.len());
            for tick in ticks {
                println!("  Token: {}, LTP: {:?}", 
                    tick.instrument_token, 
                    tick.content.last_price);
            }
        }
        TickerMessage::Error(err) => {
            eprintln!("API Key {}: Error - {}", api_key_id.0, err);
        }
        _ => {}
    }
}
```

### Per-API Channel

Get channel for a specific API key and connection:

```rust
use kiteticker_async_manager::ChannelId;

// Get channel for specific API key's first connection
if let Some(mut channel) = manager.get_channel("account1", ChannelId::Connection1) {
    while let Ok(message) = channel.recv().await {
        println!("Account1/Connection1: {:?}", message);
    }
}
```

## Monitoring

### Aggregate Statistics

```rust
let stats = manager.get_stats().await;

println!("Total API Keys: {}", stats.total_api_keys);
println!("Total Connections: {}", stats.total_connections);
println!("Total Symbols: {}", stats.total_symbols);
println!("Total Messages: {}", stats.total_messages_received);
println!("Uptime: {:?}", stats.uptime);

// Per-API statistics
for api_stat in stats.per_api_stats {
    println!("\nAPI Key: {}", api_stat.api_key_id);
    println!("  Active Connections: {}", api_stat.active_connections);
    println!("  Symbols: {}", api_stat.total_symbols);
    println!("  Messages: {}", api_stat.total_messages_received);
}
```

### Per-API Statistics

```rust
let api_stats = manager.get_api_stats("account1").await?;

println!("API Key: {}", api_stats.api_key_id);
println!("Active Connections: {}", api_stats.active_connections);
println!("Total Symbols: {}", api_stats.total_symbols);
```

### Symbol Distribution

View how symbols are distributed across API keys and connections:

```rust
let distribution = manager.get_symbol_distribution();

for (api_key_id, conn_map) in distribution {
    println!("API Key: {}", api_key_id.0);
    for (conn_idx, symbols) in conn_map {
        println!("  Connection {}: {:?}", conn_idx, symbols);
    }
}
```

## Advanced Usage

### Capacity Planning

Each API key can handle:
- **3 connections** (Kite limit)
- **3,000 symbols per connection**
- **9,000 total symbols per API key**

With multiple API keys:
```rust
// 2 API keys = 18,000 symbols capacity
// 3 API keys = 27,000 symbols capacity
// etc.
```

### Error Handling

```rust
match manager.subscribe_symbols(&symbols, Some(Mode::Quote)).await {
    Ok(_) => println!("Subscribed successfully"),
    Err(e) if e.contains("capacity") => {
        println!("All API keys at capacity, need to add more");
    }
    Err(e) => eprintln!("Subscription error: {}", e),
}
```

### Graceful Shutdown

```rust
// Stop all connections and clean up
manager.stop().await?;
```

## Best Practices

### 1. API Key Identification

Use meaningful identifiers for API keys:

```rust
.add_api_key("production_primary", api_key, token)
.add_api_key("production_backup", api_key_2, token_2)
.add_api_key("development", api_key_3, token_3)
```

### 2. Distribution Strategy Selection

- Use **RoundRobin** for balanced load across API keys
- Use **Manual** for organizational isolation or specific routing needs

### 3. Monitoring

Regularly check statistics to ensure balanced distribution:

```rust
// Periodic stats check
tokio::spawn(async move {
    loop {
        tokio::time::sleep(Duration::from_secs(60)).await;
        let stats = manager.get_stats().await;
        log::info!("Stats: {:?}", stats);
    }
});
```

### 4. Resource Management

- Start with minimum required API keys
- Add more API keys only when approaching capacity
- Monitor connection health per API key

## Examples

See the complete working example at:
- `examples/multi_api_demo.rs`

Run with:
```bash
export KITE_API_KEY_1="your_api_key_1"
export KITE_ACCESS_TOKEN_1="your_token_1"
export KITE_API_KEY_2="your_api_key_2"
export KITE_ACCESS_TOKEN_2="your_token_2"

cargo run --example multi_api_demo
```

## Comparison: Single vs Multi-API

| Feature | Single-API Manager | Multi-API Manager |
|---------|-------------------|-------------------|
| **Max Symbols** | 9,000 | Unlimited (9,000 × N API keys) |
| **API Keys** | 1 | Multiple |
| **Message Channels** | Per-connection | Unified + Per-connection |
| **Symbol Assignment** | Automatic | Automatic or Manual |
| **Use Case** | Single account | Multiple accounts, high volume |

## Troubleshooting

### Issue: "All API keys are at capacity"

**Solution**: Add more API keys or reduce symbol count

```rust
builder.add_api_key("additional_account", api_key, token)
```

### Issue: "Cannot use auto-subscribe with Manual distribution"

**Solution**: Use `subscribe_symbols_to_api` instead:

```rust
manager.subscribe_symbols_to_api("account1", &symbols, mode).await?;
```

### Issue: Messages from one API key are missing

**Check**:
1. Connection health per API key
2. Symbol distribution
3. Per-API statistics

```rust
let stats = manager.get_api_stats("account1").await?;
println!("Health: {:?}", stats);
```

## Migration from Single-API Manager

Existing code using `KiteTickerManager` works unchanged. To migrate to multi-API:

```rust
// Before: Single-API
let mut manager = KiteTickerManager::new(api_key, token, config);

// After: Multi-API
let mut manager = MultiApiKiteTickerManager::builder()
    .add_api_key("main", api_key, token)
    .base_config(config)
    .build();

// API remains similar
manager.start().await?;
manager.subscribe_symbols(&symbols, mode).await?;

// Use unified channel instead of per-connection channels
let mut unified = manager.get_unified_channel();
```

## API Reference

See [multi_api_manager.rs](../../src/manager/multi_api_manager.rs) for complete API documentation.