K
K
krumza2016-04-15 09:26:01
Node.js
krumza, 2016-04-15 09:26:01

Is it possible to cluster socket.io multi-room chat?

Good day to all!
There is such a conditional app.js construct:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
...
app.get('/create', function(req, res) { 
      var id = Math.round((Math.random() * 1000000));
      res.redirect('/room/' + id);
      addRoom(id);
});
app.get('/room/:id', function(req, res) {
    if (roomExists(req.params.id)) {
      if (getRoomByID(req.params.id).usersinroom <= max_users_count) {
        res.sendFile(__dirname + '/chatroom.html');
      } else if (getRoomByID(req.params.id).usersinroom > max_users_count) {
        req.flash('info', 'В комнате нет свободных мест')
        res.redirect('/'); 
      }    
    } else  {
      req.flash('info', 'Комнаты не существует')
      res.redirect('/'); 
    }
});
...
io.on('connection', function (socket) {
      usersCount++;
      id++;
      socket.playerid = id;     
      var addedUser = false; 

    socket.on('initme', function(data) {
      socket.join(data.roomID);
      socket.roomID = data.roomID;
      if (this.roomID != "lobby") {
      getRoomByID(data.roomID).usersinroom++;
      users = io.sockets.adapter.rooms[data.roomID].length;
      var currentmap = maps.filter(function (obj) {
        return obj.id == data.roomID;
      })[0];
       setTimeout(function () {
            socket.emit('initial', { id: id}); 
       }, 1000);   
      io.sockets.emit("entinroom", {"id": id, "room": data.roomID, "clients": users});
      }
    });
....
});

The question is - if I do pm2 start app.js -i 0, will it not turn out nasty?
Will users from different processes interact but in the same room?
Are there any ways to solve the issue of clustering an application with socket.io multi-room chat?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alastor, 2016-04-17
@Alastor

you can share sessions through radish

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question