socket.io-redis-adapter icon indicating copy to clipboard operation
socket.io-redis-adapter copied to clipboard

Redis Adapter emits message twice

Open willjk opened this issue 2 years ago • 0 comments

When I use callbacks and I follow this pattern client-1 -> server -> client-2 -> callback-to-server -> callback-to-client-1 the adapter appears to send messages twice to client-2. Here is replication code. When I comment out all redis things it will only send once

Server

`const socketIo = require("socket.io"); const redisAdapter = require("@socket.io/redis-adapter"); const redis = require("redis");

const io = new socketIo.Server( { cors: { origin: process.env.ORIGIN, methods: ["GET", "POST"], credentials: true, }, } );

let ns = io.of('/');

const pubClient = redis.createClient({ url: "redisurl" }); const subClient = pubClient.duplicate();

Promise.all([pubClient.connect(), subClient.connect()]).then(() => { io.adapter(redisAdapter.createAdapter(pubClient, subClient)); io.listen(PORT); });

pubClient.on('connect', () => { console.log('::> Redis Client Connected'); }); pubClient.on('error', (err) => { console.log('<:: Redis Client Error', err); });

ns.on("connection", async (socket) => { socket.on("host:broadcast", async (data, callback) => { const room = socket.to(data.room); room.emit("member:message", data, (arg) => { callback(arg); }); }); });

`

willjk avatar Dec 15 '22 15:12 willjk