Native WebSocket client for Kraken WebSocket API v2
This crate provides a production-ready WebSocket client for connecting to Kraken's public and private market data feeds.
Features
- Automatic reconnection with exponential backoff
- Subscription management with restoration after reconnect
- Orderbook state maintenance with checksum validation
- Event-driven architecture with async streams
Example
use kraken_ws::{KrakenConnection, ConnectionConfig, Endpoint};
use kraken_types::Depth;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = ConnectionConfig::new()
.with_endpoint(Endpoint::Public)
.with_depth(Depth::D10);
let conn = KrakenConnection::new(config);
conn.subscribe_orderbook(vec!["BTC/USD".to_string()]);
let mut events = conn.take_event_receiver().unwrap();
// Spawn connection task
tokio::spawn(async move {
conn.connect_and_run().await
});
// Process events
while let Some(event) = events.recv().await {
println!("{:?}", event);
}
Ok(())
}