Réda Youssoufi
Réda Youssoufi

Reputation: 167

Socket.io: Error: Callbacks are not supported when broadcasting at Socket.emit

I successed to send ack to android client from nodejs server but I don't succeed to do reverse. I have this error: Callbacks are not supported when broadcasting at Socket.emit

Serveur nodejs:

socket.broadcast.to(socketid).emit('message', data, callThis); 
//this function is executed when client calls it
 function callThis (dataFromClient){
console.log("Call back fired: " + dataFromClient);
 }

client android:

   socket.on("message", new Emitter.Listener() {

            @Override
            public void call(Object... args) {

                Ack ack = (Ack) args[args.length - 1];
                ack.call();

                JSONObject data = (JSONObject) args[0];
                .....

   }
}

What can I do to resolve this problem?

Upvotes: 0

Views: 4458

Answers (2)

Wei Tang
Wei Tang

Reputation: 151

Basically support the answer of @Xeoncross. When a connection came, just saved the socket into a map, like below

this.connections = new Map<string, SocketIO.Socket>()
this.server.on("connection", (socket: SocketIO.Socket) => {
      this.connections.set(socket.id, socket)
})

Then use a loop to send all users individually

public broadcast(msg: string) {
   for(const socket of this.connections.values()) {
        socket.emit("block", msg, (confirm: string) => {
             console.log("confirmation msg: ", confirm)
        })
    }
}

Upvotes: 2

Xeoncross
Xeoncross

Reputation: 57254

As the error says, "Callbacks are not supported when broadcasting". It doesn't look like you are broadcasting though, as you are trying to send to a single client. So assuming socket is an actual client socket instance you can change your code:

socket.broadcast.to(socketid).emit('message', data, callThis); 

to just send to that one person

socket.emit('message', data, callThis); 

Upvotes: 0

Related Questions