api – Establishing ~200 WebSocket connections on 2 WebSockets using Node.js and processing this data

I need data provided through Binance. This data is provided via 2 different WebSockets, I must access both at the same time. My request is to run the websocket called ticker in the background and process the most recent data on the ticker with the new data on depth each time new data is received via depth websocket.

As a result of my experiments, I learned that websockets do not cause connection problems, so we do not have ratelimit problems. Also I need to establish ~200 connections on each WebSocket. So on average, 2*200 websocket connections will be established. The same type of data should be returned on the ticker and depth websockets.

In short, I have 2 websockets and hundreds of connections, I need data coming from 2 websockets for the same type of connections.

Example of list of items (symbols) to link to:

{
 "dataJson": [
  "BTCUSDT",
  "BTCBUSD",
  "ETHUSDT",
  "ETHBUSD"
 ]
}

The following WebSocket.js file is linked to the app.js main file.

const WebSocket = require("ws")
 
const {dataJson} = require('data.json');
 
module.exports = (data, wss) => {
  for (const symbol of dataJson ) {
    try {

      const tickerWS = new WebSocket("wss://fstream.binance.com/ws/" + symbol.toLowerCase() + "@ticker");
      tickerWS.on("message", tickerData => {
        const tickerJsonData = JSON.parse(tickerData)
      });

      const depthWS = new WebSocket("wss://fstream.binance.com/ws/" + symbol.toLowerCase() + "@depth5@0ms");
      depthWS.on("message", depthData => {
        const depthJsonData = JSON.parse(depthData)
      });

    } catch (e) { console.error("Error!", e) }
  }
}

Read more here: Source link