Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simulate multiple socket.io connection

Not a duplicate of : this question, as I'm trying to use the link posted as answer to solve my problem.

I'm creating a little dummy socket client to help testing one of my product, it looks like so :

var ee = require('events').EventEmitter;
require('http').globalAgent.maxSockets = 1000;

function Dummy(){
    this.config = require('../config/credentials.js');
    this.socket = require('socket.io-client')(this.config.socketIO.url);
    var self = this;
    this.socket.on('task', function(task) {
        self.createTask(task);
    });
}

util.inherits(Dummy, ee);
module.exports = Dummy;

Dummy.prototype.createTask = function(name){
    var self = this;
    setInterval(function sendStuff(){
            self.socket.emit("msg")
    }, 1000);
};

On its own, it works fine; However, when I try to launch many of them like so :

for (var i = 0; i < 100; i++) {
    fakeClients.push(new Dummy());
};

Is appears to pool connections and shows as one client only. Based on this link, I thought that by using socket.io-client, I'd avoid the pooling behaviour, yet it doesn't work. Am I doing something wrong? I've simplified the loop btw, I actually make sure there's a delay between creations to avoid sync heartbeats.

Ideas?

like image 976
xShirase Avatar asked Dec 06 '25 11:12

xShirase


1 Answers

Found the answer, it goes like this :

function Dummy(){
    this.config = require('../config/credentials.js');
    this.socket = require('socket.io-client').connect(this.config.socketIO.url, { "force new connection": true });
    var self = this;
    this.socket.on('task', function(task) {
        self.createTask(task);
    });
}

By using the connect() function, we can set the force new connection flag to true and avoid the pooling. Simple!

like image 179
xShirase Avatar answered Dec 08 '25 01:12

xShirase



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!