binance icon indicating copy to clipboard operation
binance copied to clipboard

[USDM Futures WebSockets] Pong timeout - closing socket to reconnect

Open ChenYCL opened this issue 2 years ago • 5 comments

1|server | 'Pong timeout - closing socket to reconnect', 1|server | { 1|server | category: 'binance-ws', 1|server | wsKey: 'usdm_userData_rls9Z6p6zndsoMwx7UzxDtRXUbfpStOY0bwqeIzAzvDJMXp3KOQvhNjvozMDtv3b' 1|server | } 1|server | ] 1|server | [ 1|server | 'Closing connection', 1|server | { 1|server | category: 'binance-ws', 1|server | wsKey: 'usdm_userData_rls9Z6p6zndsoMwx7UzxDtRXUbfpStOY0bwqeIzAzvDJMXp3KOQvhNjvozMDtv3b' 1|server | } 1|server | ] 1|server | [ 1|server | 'Failed to send WS ping', 1|server | { 1|server | category: 'binance-ws', 1|server | wsKey: 'usdm_userData_rls9Z6p6zndsoMwx7UzxDtRXUbfpStOY0bwqeIzAzvDJMXp3KOQvhNjvozMDtv3b', 1|server | exception: Error: No active websocket connection exists for wsKey: usdm_userData_rls9Z6p6zndsoMwx7UzxDtRXUbfpStOY0bwqeIzAzvDJMXp3KOQvhNjvozMDtv3b 1|server | at WebsocketClient.tryWsPing (/home/robot/node_modules/binance/lib/websocket-client.js:151:23) 1|server | at WebsocketClient.sendPing (/home/robot/node_modules/binance/lib/websocket-client.js:269:14) 1|server | at Timeout._onTimeout (/home/robot/node_modules/binance/lib/websocket-client.js:181:62) 1|server | at listOnTimeout (internal/timers.js:557:17) 1|server | at processTimers (internal/timers.js:500:7) 1|server | } 1|server | ]

ChenYCL avatar Aug 13 '22 07:08 ChenYCL

Hi,

Is this on the first attempt (and it never works)? Or does this just happen sometimes?

All my websocket clients (in all my connectors) have a hearthbeat-reconnect mechanism. They send regular heartbeats to see if the connection is still active. If they don't get a reply (and they don't get any events) before a timeout, the websocket client assumes the connection dropped and starts this process (pong timeout -> closing -> reconnect -> reconnected).

It's normal for this to happen sometimes but it should otherwise be stable most of the time. If it happens a lot, it could be a slow or unreliable network (if only on one machine) or binance could be having problems (unlikely, unless it's happening on many different servers).

For when it happens sometimes, the websocket client does emit life-cycle events. You can use these to monitor when a connection opens, closed, starts reconnecting or reconnected successfully: https://github.com/tiagosiebler/binance/blob/master/src/websocket-client.ts#L117-L120

What I do in my system (e.g. in binance futures), if I see a "reconnected" event (connection dropped and reconnected successfully), I trigger a "catchup" workflow to make sure any tracked state is correct locally - using the REST API to:

  • check account balance
  • check positions
  • check orders
  • etc
wsClient.on('reconnected', async (event) => {
  try {
    if (
      typeof event?.wsKey === 'string' &&
      event.wsKey.toLowerCase().includes('userdata')
    ) {
      Logger.info(`User data WS reconnected, checking balance, positions & orders...`);
      syncBalance();
      syncPositions();
      syncOrders();
    }
  } catch (e) {
    captureException(e);
  }
});

tiagosiebler avatar Aug 13 '22 14:08 tiagosiebler

only deploy one server , after 2 days appeared the problem.

ChenYCL avatar Aug 14 '22 16:08 ChenYCL

In my experience it's normal to see this sometimes. It's only a concern if it starts to happen over and over again over longer periods of time. As I said, the connector will automatically handle issues with the websocket and will kill & recreate the websocket connection if it stops transmitting both ways.

If you're worried about missing data, I would definitely recommend using the reconnected event from the websocket client to catch up on anything you may have missed since then.

tiagosiebler avatar Aug 17 '22 09:08 tiagosiebler

hi,when client lose connect,reconnected event will re-send missing data ?

ChenYCL avatar Aug 27 '22 02:08 ChenYCL

hi,when client lose connect,reconnected event will re-send missing data ?

No, any events missed while disconnected are lost. The client will attempt to reconnect automatically if a dead connection is detected (missing heartbeat reply and no new events received before a timeout). If that happens, you can use the "reconnected" event to execute your own REST API calls to query for any missing data - see my post above for how I handle this in my own systems:


For when it happens sometimes, the websocket client does emit life-cycle events. You can use these to monitor when a connection opens, closed, starts reconnecting or reconnected successfully: https://github.com/tiagosiebler/binance/blob/master/src/websocket-client.ts#L117-L120

What I do in my system (e.g. in binance futures), if I see a "reconnected" event (connection dropped and reconnected successfully), I trigger a "catchup" workflow to make sure any tracked state is correct locally - using the REST API to:

  • check account balance
  • check positions
  • check orders
  • etc
wsClient.on('reconnected', async (event) => {
  try {
    if (
      typeof event?.wsKey === 'string' &&
      event.wsKey.toLowerCase().includes('userdata')
    ) {
      Logger.info(`User data WS reconnected, checking balance, positions & orders...`);
      syncBalance();
      syncPositions();
      syncOrders();
    }
  } catch (e) {
    captureException(e);
  }
});

tiagosiebler avatar Aug 29 '22 12:08 tiagosiebler