3

I need to have two different socket.io servers communicate with each other. I cannot use socket.io-client since it does not differentiate between browser to server connections and server to server connections. So I am trying to use socket.io-redis.

One is an express-socket.io server and another is a standalone socket.io server. Both have been configured to use socket.io-redis adapter. I do not see message received at Server1 from Server2. Also there are no errors.

Server1 :

var express = require('express');
var app = express();

var server = app.listen(8000,function () {
 console.log('server listening at port 8000');
});

var io = require('socket.io')(server);
var redis = require('socket.io-redis');
io.adapter(redis({ host: 'localhost', port: 6379 }));

io.on('message',function (message) {
 console.log(message);
});

io.on('connection',function (socket) {
 console.log('connection');
 socket.on('message',function (message) {
  console.log(message);
 });
});

Server2:

var io = require('socket.io')(3000);
var redis = require('socket.io-redis');
io.adapter(redis({ host: 'localhost', port: 6379 }));

io.emit('message','Hi');
Cyril
  • 167
  • 1
  • 4
  • 11
  • Did you check the redis store if the events written correctly? – cdagli May 03 '15 at 21:13
  • @cdagli could you have a look at http://stackoverflow.com/questions/36874287/nodejs-socket-io-server-server-communication I see from you user desc that you have some socket.io experience :). Is Koval below right that I cannot communicate server to server with socket.io-redis? – adaniluk Apr 27 '16 at 18:32

2 Answers2

3

socket.io-redis will not allow You to capture events from one server on another. How it works is it will simply "push" messages emited by one server to other connected servers, so those can emit those messages to.

If instance A will recieve an event (ie. io.on('connect')), You will not be able to capture that event on instance B. You will however be able to emit a message to all clients connected to all instances, simply by calling

socket.on('message', function(socket){
   socket.emit('hello', 'New message');
}

This way You will broadcast the message to all clients, either connected to instance A or B (including Yourself for that matter). This approach allows You to scale out Your application to other instances on one machine (utilize more than one thread) or to other servers.

If You need Your servers to "talk" to each other, You can utilize Your existing transport layer - Express server. You could create handlers for different type of requests, for example:

app.get('/api/clientCount', function(req, res){
  res.send(io.engine.clientsCount);
});

This way You can exchange information between socket.io instances, change their state, make all instances report usage, etc. Thing to remember - authenticate requests; You don't want to get those calls from unauthorised users :)

Koval
  • 145
  • 8
0

If someone still looking for a answer in 2023, in a SocketIO cluster you can communicate from one server to another using serverSideEmit as it directly communicate with other servers in the same cluster:

Sends a message to the other Socket.IO servers of the cluster.

Please note that this functionality was added in v4.1.0. Also it also worthy to take a look at serverSideEmitWithAck added in v4.6.0.

Benzamin
  • 81
  • 1
  • 9