altangent / ccxws

WebSocket client for 38 cryptocurrency exchanges
MIT License
618 stars 186 forks source link

Opening Handshake Timeouts Binance #245

Closed csidi90 closed 3 years ago

csidi90 commented 3 years ago

Hi,

I am using this wonderful module for my app and I am running into unfixable problems for me. I am subscribing to the candle streams for every USDT pair on Binance for the 5m, 30m, 1h, 4h and 1d timeframes. That quickly adds up to several thousand websocket connections which from my understanding of the binance docs is not a problem. Every combination of a Ticker + Timeframe get passed in a new ccxws.binance() instance and sets the candlePeriod.

I get opening handshake timeouts pretty quickly both locally and in production mode on heroku app and I cant seem to find out why.

Opening handshake has timed out at ClientRequest.req.on (C:\dev\cryptoalerts\server\node_modules\ws\lib\websocket.js:570:7) at ClientRequest.emit (events.js:198:13) at TLSSocket.emitRequestTimeout (_http_client.js:662:40) at Object.onceWrapper (events.js:286:20) at TLSSocket.emit (events.js:198:13) at TLSSocket.Socket._onTimeout (net.js:442:8) at ontimeout (timers.js:436:11) at tryOnTimeout (timers.js:300:5) at listOnTimeout (timers.js:263:5) at Timer.processTimers (timers.js:223:10)

` try { //this.ohlcv = await this.rest_api.fetchOHLCV(this.market.symbol, this.interval, undefined, 250) this.wsapi.candlePeriod = "" + this.interval; this.ws_api.subscribeCandles(this.market); this.ws_api.subscribeTicker(this.market) this.ws_api.on("error", err => console.error(this.market.id, err)); this.ws_api.on("candle", candle =>{ //this.checkCandle(candle); }) this.ws_api.on("ticker", ticker =>{ this.ticker = ticker;

        })

    } catch (error) {
        console.log("Symbol initialize " + this.market.symbol, error)
    }`
bmancini55 commented 3 years ago

@csidi90, thanks for submitting an issue!

That quickly adds up to several thousand websocket connections which from my understanding of the binance docs is not a problem.

Subscriptions should not a problem. Socket connections will be a problem. Socket connections are initiated via an HTTP request for which you'll be subject to the Binance's standard REST request throttling limits.

Every combination of a Ticker + Timeframe get passed in a new ccxws.binance() instance and sets the candlePeriod.

So for each market you're instantiating a new client? Each instance of the client, new ccxws.Binance(), is going to open a new connection. My suggestion would to create a client for each candle period, then subscribe to all markets using that client. So you'll end up with 5 or 6 clients / active sockets instead of thousands.