Run multiple socket.io-clients in one node.js instance

node.js

Solution

You have to pass `forceNew` option:

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

And then initialize your sockets in a loop.

Here is a full test example:

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 purpose I've made client to output incrementing number for five times each second.

Problem

I want to run stress tests using node.js, socket.io, and socket.io-client. I want to get the network bandwidth, processor/memory use, etc. I have a node.js socket.io server on amazon, size XLARGE. Now I want to run multiple socket.io-clients in other amazon servers, and connect them to my server. I was running it in different processes, but one node.js process takes 15MB memory. I want to test 100k socket.io connections simultaneously, so this is not an option. My question is: Can I run, for example, 60k different socket.io-clients in one node.js instance? 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." }); }); ```

Original source