Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

node.js

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." });
});
like image 947
Pikachu Avatar asked Jun 03 '14 10:06

Pikachu


1 Answers

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.

like image 118
Oleg Avatar answered Oct 04 '22 22:10

Oleg