altangent / ccxws

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

Mass subscription to candles #307

Closed sribna closed 2 years ago

sribna commented 2 years ago

This issue is not related to the library, rather i'm asking for an advice. My goal is getting updates for all trading pairs for each exchange. In the code below, binance.json contains several hundreds pairs

import { BinanceClient } from "ccxws";
import fs from "fs";
const binance = new BinanceClient();

let tickers = JSON.parse(fs.readFileSync('binance.json', 'utf8'));

for(let i in tickers) {
    let parts = tickers[i].split('/');
    let market = {
        id: parts[0] + parts[1],
        base: parts[0],
        quote: parts[1]
    };
    binance.subscribeCandles(market);
}
binance.on("candle", trade => console.log(trade));

I'm getting this error:

Error: Too many subscriptions
{ error: { code: 4, msg: 'Too many subscriptions' }, id: 6 }

How to solve this at the lowest possible cost?

sribna commented 2 years ago

Found this https://github.com/altangent/ccxws/issues/125 It seems to be working solution. At least i'm not getting errors anymore

let clients = [];
for(let i = 0; i < tickers.length; i++) {
    let parts = tickers[i].split('/');
    let market = {
        id: parts[0] + parts[1],
        base: parts[0],
        quote: parts[1]
    };
    if(i % 100 === 0) {
        clients.unshift(new BinanceClient());
    }
    clients[0].subscribeCandles(market);
}

for(let i = 0; i < clients.length; i++) {
    clients[i].on("candle", trade => console.log(trade));
}