Answer the question
In order to leave comments, you need to log in
How to scale a chat written in socket.io/nodejs?
Hello!
I wrote some kind of chat on socket.io and I want to scale it to two servers.
I read this this: socket.io/docs/using-multiple-nodes/#passing-event... set up nginx and now the client gets to one of these servers.
The problem is that I use a variable in each socket.io instance to store a list of the people in the room and the names of those rooms.
If one person connected to one instance, and the other to another, then they do not see each other.
From the above article I read about redis adpter. I thought that I would add these lines and my two instances would be synchronized via redis. But that did not happen.
Here is my code:
//FIRST SERVER (server1.js)
var io = require('socket.io')(3000);
var redis = require('socket.io-redis');
io.adapter(redis({ host: 'localhost', port: 6379 }));
var test = 0;
io.on('connection', function (socket) {
test+=1;
console.log("connection. test = " + test);
});
//SECOND SERVER (server2.js)
var io = require('socket.io')(4000);
var redis = require('socket.io-redis');
io.adapter(redis({ host: 'localhost', port: 6379 }));
var test = 0;
io.on('connection', function (socket) {
test+=1;
console.log("connection. test = " + test);
});
Answer the question
In order to leave comments, you need to log in
io.on('connection', function (socket) {
//
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question