1
votes

Hey guys I'm trying to implement the socket.io-redis module as per the socket.io-redis documentation. It works fine when broadcasting messages from one client to another client on the same server. But when I run two identical servers, one on port 80, and one on port 81, and try to broadcast messages between client A (connected to port 80) and client B (connected to port 81), no messages get through. This is my code:

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

io.on('connection', function(socket) {
      socket.on('initializeSocket', function(data) {
                //Assign socket id to client id lookup value
                var clientID = JSON.parse(data).clientID

                client.set(clientID, socket.id)
                console.log("%s client ID redis lookup set to:", clientID)
                console.log(socket.id)
      })

      socket.on('personalMessage-client', function(data) {

                ...

                client.get(facebookID, function(err, socketID) {
                   if(socketID) {
                   console.log('broadcasting to %s', socketID)
                   socket.broadcast.to(socketID).emit('personalMessage-server', message)
                   }
                })
      })
})

When a socket connection is initialized, the client's user ID is set as a key value pair to it's respective socket.id in redis, and is retrieved using:

client.get(facebookID, function(err, socketID) {
                   if(socketID) {
                   console.log('broadcasting to %s', socketID)
                   socket.broadcast.to(socketID).emit('personalMessage-server', message)
                   }
                })

The redis-cli monitor indicates that messages are in fact being published, and it is my understanding that if both websocket servers are connected to the same redis server, websocket messages should be able to be passed between servers with different addresses.

However, the client A receives no messages from client B when using two identical servers on different ports. Client A is only able to receive messages if it is connected to server with the same port as client B.

If anyone can help that would be much appreciated, and for all I know I'm using the socket.io-redis adapter module totally wrong.

1

1 Answers

1
votes

Try passing in subClient and pubClient, that works fine for me.

var redisAdapter = require('socket.io-redis');
var redis = require('redis');
var pub = redis.createClient('6379', '127.0.0.1');
var sub = redis.createClient('6379', '127.0.0.1');
io.adapter(redisAdapter({
    key: 'adapterKey',
    pubClient: pub,
    subClient: sub,
}));

Also looking at the screenshot you posted, seems like the port numbers are in the range 62000+, is this your redis port(s)? You are sending in 6379(the default port) for the socket.io-redis module.