Run multiple .io client sockets in a single instance of node.js

I want to run stress tests using node.js, socket.io and socket.io-client.

I want to get network bandwidth, CPU / memory usage, etc.

I have a node.js socket.io server on amazon, size is XLARGE.

Now I want to run several .io client sockets on other amazon servers and connect them to my server.

I ran it in different processes, but one node.js process takes 15 MB of memory. I want to test simultaneous socket connections 100.io, so this is not an option.

My question is: can I run, for example, 60k different .io client sockets in one instance of node.js?

This is my client code:

var socket = require('socket.io-client')('http://someAddress:3000'); socket.on('connect', function(){ socket.on('news', function(data){ }); socket.emit('event', { data: "Connection test." }); }); 
+6
source share
1 answer

You need to pass the forceNew option:

 var socket = require('socket.io-client')('http://someAddress:3000', { forceNew: true }); 

And then initialize your sockets in a loop.

Here is a complete test case:

server.js:

 var io = require('socket.io')(); io.on('connection', function(socket){ var i = 0; setInterval(function() { socket.emit('news', { message: i++ }); }, 1000); }); io.listen(3000); 

client, js:

 function testOne() { var socket = require('socket.io-client')('http://127.0.0.1:3000', {forceNew: true}); socket.on('connect', function(){ socket.on('news', function(data){ console.log(data.message); }); }); } for (var i = 0; i < 5; i++) { testOne(); } 

For testing purposes, I forced the client to output an increasing number five times per second.

+10
source

Source: https://habr.com/ru/post/970233/


All Articles