Scalable architecture for socket.io

I am new to socket.io and Node JS, and I am trying to create a scalable application with many simultaneous socket connections (10,000 +).

Currently, I started with a model where my server creates a child process, and each child process listens on a specific port with an attached instance of sicket.io. When a client connects, it is redirected to a specific port.

The big question is: do multiple socket.io instances on multiple ports have the number of possible connections?

Here is my code, just in case:

Server

var server = http.createServer(app);

server.childList = [];
for (var i = 0; i < app.portList.length; i++) {
  server.childList[i] = require('child_process').fork('child.js');
}

server.listen(3443, () => {
  for (var i = 0; i < app.portList.length; i++) {
    server.childList[i].send({ message: 'createServer', port: app.portList[i] });;
  }
});

child.js:

var app = require('./app');
var http = require('http');
var socket_io        = require( "socket.io" );

process.on('message', (m) => {
    if (m.message === 'createServer') {

        var childServ = http.createServer(app);

        childServ.listen(m.port, () => {
            console.log("childServ listening on port "+m.port);
        });

        var io = socket_io();
        io.attach( childServ );

        io.sockets.on('connection', function (socket) {
            console.log("A client just connected to my socket_io server on port "+m.port);
        });
    }
});

Feel free to free kraken if I did something terrible there

+4
source share
2

-, , , , socket.io - . , node.js -, . , , ( ), .

(, , , ) ( ). , , . .

, N socket.io . , - , , (, ).

. N , , - loadbalancer . , , , . , .

socket.io : Socket.io - , redis socket.io redis, socket.io .

+5

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


All Articles