wisespace-io / binance-rs

Rust Library for the Binance API
Other
665 stars 297 forks source link

Unable to connect to more than one markets orderbooks #15

Closed JoshuaBatty closed 6 years ago

JoshuaBatty commented 6 years ago

Hi sorry to bother you again @wisespace-io ... i've been pulling my hair out this morning trying to connect to more that one markets orderbook stream.

A small example is something like this

    web_socket.add_market_handler(stream.clone());
    let partial_depth1: String = format!("{}@depth", "ethbtc");
    let partial_depth2: String = format!("{}@depth", "xrpbtc");

    web_socket.connect(&partial_depth1).unwrap(); // check error
    web_socket.connect(&partial_depth2).unwrap(); // check error
    web_socket.event_loop();

I event tried copying the market_websocket() method in the binance_websocket example, gave the two methods different names, e.g market_websocket1() & market_websocket2(). Inside each one I tried to spawn a new market. However only one market only ever gets printed to the console.

My end goal is to have a websocket connection to every market on binance and receive their orderbooks. Am I missing something super obvious or currently can this crate only connect and stream orderbooks from only ever a single market?

Appreciate any advice. Cheers.

JoshuaBatty commented 6 years ago

I might add, that after collecting every market into a HashMap and doing the following loop (as you can do in bitfinex-rs completely fine) takes about 10 minutes to get through the whole loop.

It seems the call to connect() blocks for some time, as opposed to the subscribe_books() method in bitfinex-rs... Is this a limitation on this crate or binance's side?

    for (symbol, _) in stream.pairs {
        let partial_depth: String = format!("{}@depth{}", symbol.to_lowercase(), 20);
        web_socket.connect(&partial_depth).unwrap(); // check error
    }
wisespace-io commented 6 years ago

@JoshuaBatty The binance websockets service is not based on Subscription as the Bitfinex api (Only one connection). You would need to spawn different threads per connection/eventloop. We would need to investigate a better war of doing it.

wisespace-io commented 6 years ago

@JoshuaBatty I looked at the Binance websockets api and I noticed that they support combined streams. We would need to add support for it, it would solve your issue as you could do multiples requests in a same connection (url).

/stream?streams =<streamName1>/<streamName2>/<streamName3>

Right now we only support raw (single) stream:

/ws/<streamName>

Reference: https://github.com/binance-exchange/binance-official-api-docs/blob/master/web-socket-streams.md

JoshuaBatty commented 6 years ago

Thanks @wisespace-io I have it working with binance-rs already :) The following code works just fine with this crate as it already is. I'm happy to close this issue now. Thanks again!

    let mut depth_stream = String::new();
    for (symbol, _) in stream.pairs {
        let depth_chan: String = format!("{}@depth", symbol.to_lowercase());
        depth_stream.push_str(&depth_chan);
        depth_stream.push_str("/");
    }
    web_socket.connect(&depth_stream).unwrap(); // check error
wisespace-io commented 6 years ago

Good to know. It is probably good to update the examples in case someone else ask again.