socket.io - how to broadcast messages on a namespace?

前端 未结 4 982
一生所求
一生所求 2020-12-12 14:30

According to socket.io examples:

To broadcast, simply add a broadcast flag to emit and send method calls. Broad

4条回答
  •  天涯浪人
    2020-12-12 14:50

    Seems I was able to solve this for myself after opening a bounty. Sorry about that.

    Anyway, see if this helps:

    chat.on('connection', function (socket) {
      socket.on('message', function (msg) {
        socket.emit(msg); // Send message to sender
        socket.broadcast.emit(msg); // Send message to everyone BUT sender
      });
    });
    

    However, you could save some bandwidth and create a more snappy experience for users if you don't resend it to the sender. Just add their messages directly to the chat log, and optionally use use only self-emit to confirm it was received without issue.

提交回复
热议问题