socketio / socket.io-redis-adapter

Adapter to enable broadcasting of events to multiple separate socket.io server nodes.
https://socket.io/docs/v4/redis-adapter/
MIT License
2.74k stars 489 forks source link

I get "Callbacks are not supported when broadcasting" error when implementing acks #547

Closed knixer closed 1 month ago

knixer commented 1 month ago

I'm trying to emit data (with ack) to a specific socket based on socket id. I use redis-adapter and I want to be able to send the message even if the other user is connected to another node.

I use io.of(namepsace).to(socketId).emit(event, data, (ack) => { ...code })

But I get the error: Callbacks are not supported when broadcasting.

Copilot gives me a solution where I should use:

const socket = io.sockets.sockets.get(socketId)
socket.emit(event, data, (ack) => { ...code })

but io.sockets.sockets.get(socketId) returns undefined since io.sockets.sockets is an empty Map io.of(namespace).sockets only returns sockets from the current node, not all nodes. the io.of(namespace).adapter.fetchSockets() gives an error: Cannot read property 'rooms' of undefined

I got recommended to use serverSideEmit. which should be usable with the socket.io and socket.io-redis versions I use but I get the error: "TypeError; io.serverSideEmit is not a function"

How do I implement acks for more robust communication?

I currently use socket.io v4.0.0 and socket.io-redis v6.1.0.

Please help

knixer commented 1 month ago

Used the new @socket.io/redis-adapter package and got serverSideEmit to work. However, you can't send a socket in a callback. Now I use a solution including io.of(namepsace).fetchSockets(). However the same problem occurs when emitting with ack (Callbacks are not supported when broadcasting). I guess then using acks is not possible when on a multi-node solution like @socket.io/redis-adapte offers.

Closed Issue