tiagosiebler / binance

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

Add support for futures WS API #444

Open Luke-Rogerson opened 2 months ago

Luke-Rogerson commented 2 months ago

The new Binance API documentation specifies a different websocket endpoint to the one specified in your base URLs.

Do you know if your one is still valid? If not, is there a planned cutover time? Binance certainly don't write the clearest documentation!

tiagosiebler commented 2 months ago

Hi @Luke-Rogerson - yes, the current one is still valid. There's actually two difference concepts here and the naming of the "new" one isn't the most obvious thing.

There's the traditional websockets (event emitters, binance is a producer, you're a consumer) where you subscribe to topics/channels and receive data (one-way), then there's a newer websocket that can be used a bit like a REST API. This is what binance has called WebSocket API. This nifty new system allows you to send orders and other commands over a websocket connection, shaving off some of the latency you would see from a traditional REST API.

If you're looking to consume events (market data, account events, etc), that's fully supported as is with the current WebSocket client. That's either the WebSocket Market Streams, listed here for USDM futures: https://developers.binance.com/docs/derivatives/usds-margined-futures/websocket-market-streams

Or the user data stream (for account events), listed here: https://developers.binance.com/docs/derivatives/usds-margined-futures/user-data-streams

I have some minimal examples for both types of events: https://github.com/tiagosiebler/binance/blob/master/examples/ws-userdata.ts#L128-L131 https://github.com/tiagosiebler/binance/blob/master/examples/ws-public-usdm-funding.ts#L68

All consumer websockets are wrapped up in convenience functions in the ws client, you can see them here: https://github.com/tiagosiebler/binance/blob/master/src/websocket-client.ts#L1211-L2072

If you're not sure which you need, I'd suggest to check the docs to see which websocket topic/channel has what you want, then search in the websocket-client to see if there's a function mapped to it. We're still in the process of adding some of the newer ones, so if you see anything you're missing - let us know or submit a PR. Otherwise we'll get to updating it all eventually.

tiagosiebler commented 2 months ago

Also the WebSocket API is a curious one. While it's not supported in this SDK for binance yet, I will be adding support for it soon. My gate.io SDK is actually the first one to see an implementation of a WS API and I'm likely to bring the same design over to the binance SDK (as well as my other SDKs for exchanges that have a WS API). So if you'd like to see how that can work in practice, take a look at the gate SDK. Some docs here: https://github.com/tiagosiebler/gateio-api/blob/master/README.md#websocket-api

Also an example here where you can see a minimal implementation in action: https://github.com/tiagosiebler/gateio-api/blob/master/examples/ws-private-spot-wsapi.ts#L88-L95

It's a great time for feedback/concerns, if you'd like to influence how the final implementation will look/work, so if this is something you will use in future please do take a look and ideally try it out.