M
M
miki1312015-09-22 20:53:59
Node.js
miki131, 2015-09-22 20:53:59

How to make a chat cluster?

I am writing a chat that will be with a large number of connections.
The chat requires checking users for a ban.
In order to keep many connections I use cluster

var cluster = require('cluster'),
    numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
    for (var i = 0; i < numCPUs; i++) {
        cluster.fork();
    }
}

if (cluster.isWorker) {
    var io = require('socket.io').listen(2001);

    io.sockets.on('connection', function (socket) {
        console.log('connected to worker id ', cluster.worker.id);
    });
    io.sockets.on('user-message', function (socket) {
        // здесь не знаю что делать
    });
}

After the user sends a message, I need to check if the user is banned.
I imagined that there would be a common object
USERS = {
 1: {id: 1, banned: false},
 2: {id: 3, banned: true}
 ......
}

and at each event check it for a ban.
How to do it and where to store this object?
There is such an idea - to store it in the master and from the "worker" through process.send (); send everything that came to the socket, in the same place, in the master, check and broadcast to everyone.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
webbus, 2015-09-22
@webus

Wang problems with handshake :) I advise you to immediately take a look at https://github.com/indutny/sticky-session
On the issue, store in Redis.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question