I was inspired by this: http://socket.io/docs/using-multiple-nodes/#passing-events-between-nodes, and right now I want to synchronize my two socket.io instances through the redis adpter.
This 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);
});
When I connecting to server1.js (port 3000) - I see 'connection. test = 1', it's good, but the console of the second server is still clean. I want second server (port 4000) to do the same (print 'connection = 1').
What I'm doing wrong? Can you show me an example how to use the adapter?
Thanks
If you only connect to server1:3000
there's no way the io.on('connection', ...)
would be triggered on server2:4000
- after all, you are not connected to that server.
Each client will connect to only one of your servers. If you were not using the Redis adapter clients connected to different servers would be unable to communicate. Now with the Redis adapter the servers know about the clients of each other and can broadcast messages to all connected clients of all servers.