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

Because of ping/pong - Binance drops connection with error 1006 #304

Closed mr-smit closed 1 year ago

mr-smit commented 1 year ago

How to replicate:

Connect to 20 symbols and fetch: subscribeSymbolBookTicker spot+usdm subscribeKlines 1m spot+usdm

by default ping pong is: pongTimeout: 7500, pingInterval: 10000,

But binance has a LIMIT

WebSocket connections have a limit of 10 incoming messages per second. A message is considered:
A PING frame
A PONG frame
A JSON control message (e.g. subscribe, unsubscribe)

because "binance npm" are creating separate socket to server, we will have over 20*4=80 connections, and every connection will send a ping/pong frame, so we will send 80 ping pong and the limit is reached and binance is dropping our connections, this will triger massive reconnections

right now I thinking how to bypass this limitation.. something like creating a loop with different timeout parameters, multi thread or something custom.. thinking..

tiagosiebler commented 1 year ago

This incoming message per second is per connection.

A connection that goes beyond the limit will be disconnected

Since each topic is a separate connection, with the default ping/pong you should never be able to reach this limit (one ping frame every 7.5 seconds). In one system of mine I have approx 250 individual websocket connections through this connector and they rarely disconnect thanks to this heartbeat mechanism. If you're seeing regular disconnections you should check the logs if you're seeing a heartbeat/pong timeout or something else is disconnecting the connection.

Afaik binance also checks that you consume every event you receive, and if there's backpressure (they're sending events faster than you're processing them), they might also disconnect the connection (which the library should reconnect automatically).