I'm new to socketio-redis adapter. Originally I had a single socketio process, but now trying a socketio cluster.
When it was a single process, I'd maintain a Map of connected sockets, to find a socket by a userId. When user A emit to a room, if user B was connected but not yet in the room, B's socket was found and joined that room. Then A's socket emit to room.
My question is, how to go about this with socketio-redis?
Below is a simplified plan. Is it possible to add a userId to a socket on connect in one socketio process, then find that socket by userId in a different process?
chatio.on('connect', socket => {
// add userId to socket obj
socket.userId = userId
. . .
socket.on("message", async (data, ack) => {
const userId = data.otherUserId
const roomId = data.roomId
const message = data.message
const clients = await io.of('/').adapter.clients()
// a message came in from user A
// if user B socket is found, B joins same room
const otherUserSocket = clients.find(socketId => {
const s = io.of('/').adapter.connected[socketId]
return (s.userId == userId)
})
if (otherUserSocket) {
otherUserSocket.join(roomId)
socket.to(roomId).emit("message", { message })
}
}
})
Anyone have experience implementing something like this?