use barter_data::streams::reconnect::stream::ReconnectingStream; use barter_data::{ exchange::binance::futures::BinanceFuturesUsd, streams::Streams, subscription::trade::PublicTrades, }; use barter_integration::model::instrument::kind::InstrumentKind; use futures_util::StreamExt; use tracing::{info, warn}; #[rustfmt::skip] #[tokio::main] async fn main() { // Initialise INFO Tracing log subscriber init_logging(); // Initialise PublicTrades Streams for BinanceFuturesUsd only // '--> each call to StreamBuilder::subscribe() creates a separate WebSocket connection let streams = Streams::::builder() // Separate WebSocket connection for BTC_USDT stream since it's very high volume .subscribe([ (BinanceFuturesUsd::default(), "btc", "usdt", InstrumentKind::Perpetual, PublicTrades), ]) // Separate WebSocket connection for ETH_USDT stream since it's very high volume .subscribe([ (BinanceFuturesUsd::default(), "eth", "usdt", InstrumentKind::Perpetual, PublicTrades), ]) // Lower volume Instruments can share a WebSocket connection .subscribe([ (BinanceFuturesUsd::default(), "xrp", "usdt", InstrumentKind::Perpetual, PublicTrades), (BinanceFuturesUsd::default(), "sol", "usdt", InstrumentKind::Perpetual, PublicTrades), (BinanceFuturesUsd::default(), "avax", "usdt", InstrumentKind::Perpetual, PublicTrades), (BinanceFuturesUsd::default(), "ltc", "usdt", InstrumentKind::Perpetual, PublicTrades), ]) .init() .await .unwrap(); // Select and merge every exchange Stream using futures_util::stream::select_all // Note: use `Streams.select(ExchangeId)` to interact with individual exchange streams! let mut joined_stream = streams .select_all() .with_error_handler(|error| warn!(?error, "MarketStream generated error")); while let Some(event) = joined_stream.next().await { info!("{event:?}"); } } // Initialise an INFO `Subscriber` for `Tracing` Json logs and install it as the global default. fn init_logging() { tracing_subscriber::fmt() // Filter messages based on the INFO .with_env_filter( tracing_subscriber::filter::EnvFilter::builder() .with_default_directive(tracing_subscriber::filter::LevelFilter::INFO.into()) .from_env_lossy(), ) // Disable colours on release builds .with_ansi(cfg!(debug_assertions)) // Enable Json formatting .json() // Install this Tracing subscriber as global default .init() }