Petr Shypila Petr Shypila - 3 days ago 5
Javascript Question

How to open a pool of websocket connections

I need to perform a load testing on our websocket service.
Is there a way how can I open a bunch of websocket connections from a single workstation?

I already tried npm's

ws
and
websocket
modules to launch them with NodeJS. It works fine with a single connection, but when I try to open them in a for-loop it throws an exception or uses only last open client.

Answer

If you want to do it from a single workstation, you can use child processes :

app.js :

var cp = require('child_process');
var children = [];
var max = 10; // tweak this to whatever you want
for(var i = 0; i<max; i++){
    children[i] = cp.fork('./child.js');
    .on('error', function(e) {
        console.log(e);
    }).on('message',function(m){
        console.log(m)
    }).on('exit', function(code) {
      console.log('exit with code',code);
    });
}

then in child.js, just have a script that starts a connection. you can also send messages between parent and child, using the child process API

Comments