tiagosiebler / binance

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

Some Events are not firing #365

Closed vikt20 closed 8 months ago

vikt20 commented 9 months ago

const ws = await binance.subscribeUsdFuturesUserDataStream();

These events are firing

ws.on('open', () => console.log(Connected to Binance)); ws.on('close', () => console.log(Closed connection)); ws.on('message', (data) => console.log(Message:, data));

These are not

ws.on('reconnecting', () => console.log(Connecting to Binance)); ws.on('reconnected', () => console.log(Reconnected to Binance)); ws.on('formattedMessage', (data) => console.log(FormattedMessage:, data)); ws.on('formattedUserDataMessage', (data) => console.log(FormattedUserDataMessage:, data));

Testing (close ws with auto reconnection)

setTimeout(() => binance.closeWs(ws, true), 3000)

What am I doing wrong? I appreciate your help.

tiagosiebler commented 9 months ago

You should not really use the websocket returned by these subscribe*() calls. That WebSocket only lives for that connection, and if a reconnection happens, it will be replaced. All event handling should be on your instance of the websocket client itself. Take a look at the example here: https://github.com/tiagosiebler/binance/blob/master/examples/ws-public.ts#L34-L47

Note that if you want formatted events, you should also make sure to include beautify: true in the wsclient options, as shown in this example as well.

tiagosiebler commented 8 months ago

Will close for now, feel free to reopen if you have more questions or issues here