Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subscribe on multiple BookTickerStream dynamically #9

Closed
raeisimv opened this issue Nov 22, 2023 · 2 comments · Fixed by #10
Closed

Subscribe on multiple BookTickerStream dynamically #9

raeisimv opened this issue Nov 22, 2023 · 2 comments · Fixed by #10

Comments

@raeisimv
Copy link
Contributor

raeisimv commented Nov 22, 2023

Subscribing to multiple symbols can be done as follows:

let mut conn = BinanceWebSocketClient::connect_with_url(&stream_adr)?;
conn.subscribe(vec![
    &BookTickerStream::from_symbol("BTCUSDT").into(),
    &BookTickerStream::from_symbol("BNBBUSD").into(),
]);

It works fine, however, I need to subscribe to a dynamic list of symbols (based on the app's settings).

let symbol_lst = vec!["BTCUSDT".to_string(), "BNBBUSD".to_string(), "BTCUSDT".to_string()];

let lst = symbol_lst.iter()
    .map(|x| BookTickerStream::from_symbol(&x))
    .collect::<Vec<_>>()
    ;

// try 1
conn.subscribe(lst.into_iter().map(|x| x.into()));
// ^^^^ the trait `From<BookTickerStream>` is not implemented for `&binance_spot_connector_rust::websocket::Stream`

// try 2
conn.subscribe(lst.into_iter().map(|x| &x.into()));
// returns a reference to data owned by the current function

Description

The issue comes from failing to convert BookTickerStream into stream (impl From<BookTickerStream> for Stream).
There is no public API to create websocket::Stream, so it is required to use the converter.

@stupid-boar
Copy link

stupid-boar commented Feb 19, 2024

you can start the subscription sequentially as a temporary solution:

// let conn = Arc<Mutex<WebSocketState>>;
join_all(symbols.iter().map(|symbol| {
  let conn = conn.clone();
  async move {
    conn.lock().await.subscribe(vec![&TradeStream::new(&symbol).into()]).await
  }
})).await;

@alplabin
Copy link
Contributor

It has been added in latest release: https://github.com/binance/binance-spot-connector-rust/releases/tag/v1.2.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants