tiagosiebler / binance

Node.js & JavaScript SDK for Binance REST APIs & WebSockets, with TypeScript & browser support, integration tests, beautification & more.
MIT License
763 stars 265 forks source link

Add support for combined ws streams #143

Open tiagosiebler opened 3 years ago

tiagosiebler commented 3 years ago

There's two types of combined streams to support, each with a diff use case. The first one is easier than the second:

Stretch goals for dynamic combined streams

MrAlvaroPS commented 2 years ago

Cheers!!

I'm not sure how to create a combined stream with this API.

I think I just made a workaround that makes it, but maybe you have it previously considered and can throw me a bone here.

I'm trying to reach all 5m klines available in one stream. I've done it before directly with Binance API but I like the way you parse the response, so I made this:

image.

Is there a better way to do this?

tiagosiebler commented 2 years ago

This seems like a way to do it. For the wskey, I would recommend to include all dynamic props...maybe something like symbols.sort().join().replace('USDT') isn't too long of a string? Reasoning is if you want to have multiple calls to this with diff symbols, you don't want any risk for wskeys to get mixed up. They should be unique, or when a reconnect happens you'll get the wrong websocket url being reconnected.

maybe streamName (in getWsKeyWithContext) would also be good as something suggestive like kline_multiple. Might need some testing to see how the reconnect logic behaves if your internet connection (and thus the stream) drops.

MrAlvaroPS commented 2 years ago

Cheers!

Done that and so far so good. I'll keep an eye on it and tell you if any errors comes up.

Thanks again for your work!!

tiagosiebler commented 2 years ago

Glad to hear! Would you mind sharing the snippet you're currently using? Are reconnects working OK? Would be great to add it to the lib.

MrAlvaroPS commented 2 years ago

Yeah, already tried the reconnects and it went all fine.

We were just talking in Telegram. I can send you anything else you need.

The snippet to connect all the cryptos at the same time:

`/**

`/**