tiagosiebler / binance

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

Pong timeout - After 3 Minutes #287

Closed joshelboy closed 1 year ago

joshelboy commented 1 year ago

First - Thank you for the great handler/package!

I am currently facing a "Pong Timeout" every 3 minutes after connecting to a binance ws endpoint. It tries to reconnect from then, what isn't exactly the problem - My problem is, that this is happening after exactly 3 minutes... Every time!

My console looks like this:

[
  'Websocket connected',
  { category: 'binance-ws', wsKey: 'spot_ticker__' }
]
connection opened open: spot_ticker__ wss://stream.binance.com:9443/ws/!ticker@arr

# and after 3 minutes

[
  'Pong timeout - closing socket to reconnect',
  {
    category: 'binance-ws',
    wsKey: 'spot_ticker__',
    reason: 'Pong timeout'
  }
]

My code looks like this:


const wsClient = new WebsocketClient(
    {
        api_key: API_KEY,
        api_secret: API_SECRET,
        beautify: false,
        // Disable ping/pong ws heartbeat mechanism (not recommended)
        disableHeartbeat: false,
    }
);

// ....

// receive raw events
        wsClient.on('message', (data) => {
            console.log(data);
        });

// notification when a connection is opened
        wsClient.on('open', (data) => {
            console.log('connection opened open:', data.wsKey, data.ws.target.url);
        });

// read response to command sent via WS stream (e.g LIST_SUBSCRIPTIONS)
        wsClient.on('reply', (data) => {
            console.log('log reply: ', JSON.stringify(data, null, 2));
        });

// receive notification when a ws connection is reconnecting automatically
        wsClient.on('reconnecting', (data) => {
            console.log('ws automatically reconnecting.... ', data?.wsKey);
        });

// receive notification that a reconnection completed successfully (e.g use REST to check for missing data)
        wsClient.on('reconnected', (data) => {
            console.log('ws has reconnected ', data?.wsKey);
        });

// Recommended: receive error events (e.g. first reconnection failed)
        wsClient.on('error', (data) => {
            console.log('ws saw error ', data?.wsKey);
        });

        wsClient.subscribeSpotAll24hrTickers();
tiagosiebler commented 1 year ago

Weird...this specific topic should be emitting data every 1000ms. Are you still receiving data just before the pong timeout? Or do the ticker updates also stop happening?

The pong timeouts are designed to expect either a pong from the exchange OR any event on that ws: https://github.com/tiagosiebler/binance/blob/master/src/websocket-client.ts#L357-L363

If you saw a pong timeout, it would suggest you also stopped receiving data (which makes this forced reconnect a necessary one)..

joshelboy commented 1 year ago

Omg, I am so sorry. It was my DB that couldn't handle all the inserts - But I don't really understand why the WS disconnects there instead of my DB? Because my DB-Handler didn't alert me on anything... But I changed to mass insertion and it fixed my problem.

Anyways: Keep up the great work! Closing this now

tiagosiebler commented 1 year ago

weird...would be curious how that can interfere pong timer flushing...but glad you found the problem & that my lib is helping!