Nodejs Socket.io v4 disconnection problem on 1000 connections with 100 subscription each
I am trying to create one application using nodejs socket.io with version 4.
I am using very simple script that is accepting a connection and send a JSON dummy ({"abc":"abc"}) payload on an event(say 'loaddata' event) into a socket room which is created on event call(if it is not already created).
While benchmarking, if i try to make 1000 connections with 100 common subscription(100 rooms), then it starts giving disconnection error of few socket connections after few seconds.
Server(where socket code is running) Machine details : 8cpu,8Gb RAM.
After TOP command usage, it seems , CPU is over 100% when i shoot 1000 connections in a go with 100 subscription each.
is there some limitation in socket.io or i am doing some mistake or is this is the max limit for socket.io with this conn number. ?
//Server code :
const app = require('express')();
const server = require('http').Server(app);
const {
Server
} = require("socket.io");
const io = new Server(server, {
path: '/abc',
transports: ['websocket']
})
io.on('connection', async (socket) => {
socket.emit('broadcast', `Welcome to StreamerIO `)
socket.on('dataload', (data) => {
socket.join('datafeed_' + data.token);
sendFeed('datafeed_' + data.token);
});
});
const sendFeed = (room) => {
io.to(room).emit("getdata", {
"abc": "abc"
});
}
server.listen("7005", () => {
console.log(`Socket Server listening on ${server.address().address}:${server.address().port}`);
});
//Client Code :
const io = require("socket.io-client");
const connectFeed = async (subscription, tokens_list) => {
const socket = io('ws://localhost:7005/', {
path: "/abc",
transports: ['websocket']
});
socket.on("connect", () => {
function subscribetoken() {
if (socket.connected) {
if (tokens_list && tokens_list.length) {
tokens_list.forEach(token => {
socket.emit("dataload", {
token: token
});
});
}
}
}
function pingpong() {
socket.emit("3");
}
setTimeout(subscribetoken, 1000)
setInterval(pingpong, 1000)
});
socket.on('getdata', function(message) {
console.log("received data");
});
}
const startBenchMarking = (connections, subscription, tokens_list) => {
var runConnections = () => {
for (let i = 1; i <= connections; i++) {
connectFeed(subscription, tokens_list)
}
}
runConnections();
}
const NO_OF_CONNECTIONS = 1000;
const NO_OF_SUBSCRIPTIONS = 100;
const activetokens = ["a|1", "a|2", "a|3", ........] // an array of 100 diff tokens.
startBenchMarking(NO_OF_CONNECTIONS, NO_OF_SUBSCRIPTIONS, activetokens);
i tried running above scripts with diff no of connections. it is running fine with upto 950 connections. and getting disconnections problem when connections crosses 1000+.
expecting : is there some limitation in socket.io or i am doing some mistake or is this is the max limit for socket.io with this conn number. ?
Answers
It's unlikely that there's an inherent limitation in Socket.IO itself causing the disconnection issues. More likely, it's a combination of factors related to your server's resources, network conditions, and possibly how your code is handling the connections.
Here are a few things you can consider to troubleshoot and optimize your setup:
-
Server Resources: While your server has decent resources, handling 1000 connections simultaneously can still strain it, especially if each connection is actively sending and receiving data. Monitor your server's CPU, memory, and network usage during the benchmarking to see if any of them are hitting limits. You might need to optimize your code or consider scaling up your server resources if necessary.
-
Connection Handling: Make sure your server code efficiently handles incoming connections and events. For example, avoid synchronous operations that could block the event loop, optimize memory usage, and consider implementing connection pooling if applicable.
-
Network Conditions: Ensure that your server's network configuration can handle the incoming connections and traffic. Check for any network bottlenecks or issues that could affect the performance.
-
Socket.IO Configuration: Review your Socket.IO server configuration to see if any settings need adjustment. For example, you might need to adjust timeouts, max listeners, or other parameters to better suit your use case.
-
Error Handling: Implement robust error handling in your server code to gracefully handle any errors or exceptions that may occur during the connections. This can help prevent crashes and unexpected behavior.
-
Load Testing: Consider using a dedicated load testing tool or service to simulate high loads and analyze how your server behaves under stress. This can help identify any performance bottlenecks or issues that may not be apparent during regular testing.
-
Code Optimization: Review your server and client code for any potential optimizations or inefficiencies. Look for areas where you can reduce unnecessary processing, improve concurrency, or optimize network communication.
By systematically reviewing and addressing these areas, you should be able to improve the stability and performance of your Socket.IO application, allowing it to handle larger numbers of connections more effectively.