While I have essentially completed the project, I need to use socket connections to ensure that the data remains up-to-date and that I can handle frequent connection errors. I have some questions as I evaluate my current solutions, and I've listed these questions. Additionally, if there is a better solution or architectural suggestion, I would be happy to hear about it. The main Node.js application retrieves data from Redis, so I'm considering handling only the socket part with GO and transferring the data to Redis. JavaScript sometimes makes me feel challenged with complex methods. Do you think it's the right approach to ask the following questions and evaluate the situation in this way?
In Node.js applications, what's the most suitable loop mechanism for socket connections? Is using
setIntervalmore advantageous, or awhileloop? What are the differences in terms of performance and error handling?When using CCXT Pro's
watchOrderBookmethod inside awhileloop, does each call to this method initiate a new connection request to the exchange, or does it maintain a connection and receive updates automatically after the initial connection?How should the architecture be for connecting to multiple exchanges and servers for socket connections? I am storing the exchange and symbol information with server numbers. How can I issue socket listening commands to these servers based on the known information?
Is the exponential backoff mechanism necessary for reconnecting sockets when a disconnection occurs? Would there be any issues if a fixed reconnection time, say every 30 seconds, is set instead? Why is the exponential backoff approach preferred by others, I am curious to know.
My current working method is as follows, but as a last resort, I will improve this example to establish connections. In this case, I am also concerned about excessive resource consumption caused by the while loop. Additionally, this method hinders my ability to manage dropped connections because there will be times when I need to establish and terminate connections. While I can achieve this with setInterval it doesn't seem possible with while, I believe.
async function watchSelected(symbol) {
let data;
setInterval(async () => {
console.log(data?.asks[0][0]);
}, 1000);
while (true) {
const result = await exc.watchOrderBook(symbol, 10);
data = result;
}
}
watchSelected();
In a Node.js application for socket connections:
setIntervalwhen simplicity and fixed intervals are sufficient.whileloop for more control and dynamic scheduling.Choose based on your application's requirements and complexity.