socketio / socket.io-redis-emitter

The Socket.IO Redis emitter, allowing to communicate with a group of Socket.IO servers from another Node.js process.
https://socket.io/docs/v4/redis-adapter/
MIT License
722 stars 121 forks source link

Can several servers use the same redis? #83

Closed startmaster closed 3 years ago

startmaster commented 5 years ago

Several servers use the same redis,but db is different.How can one server send to all its clients but not other servers' clients? I test it.For example,i send message in server A,but server B‘s clients receive this message.Because A and B use the same redis

md-seb commented 4 years ago

Configure the key option when instantiating. It defaults to 'socket.io'

darrachequesne commented 3 years ago

@md-seb is right, you can use a different key for each Socket.IO server:

Server A

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

Server B

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

And then the emitter instances:

const emitter1 = require('socket.io-emitter')({ host: '127.0.0.1', port: 6379, key: 'socket.io' }); // default value
const emitter2 = require('socket.io-emitter')({ host: '127.0.0.1', port: 6379, key: 'custom' });