How to send a message to a particular client with socket.io

前端 未结 6 1997
無奈伤痛
無奈伤痛 2020-11-28 00:02

I\'m starting with socket.io + node.js, I know how to send a message locally and to broadcast socket.broadcast.emit() function:- all the connected clients recei

6条回答
  •  温柔的废话
    2020-11-28 00:56

    You can refer to socket.io rooms. When you handshaked socket - you can join him to named room, for instance "user.#{userid}".

    After that, you can send private message to any client by convenient name, for instance:

    io.sockets.in('user.125').emit('new_message', {text: "Hello world"})

    In operation above we send "new_message" to user "125".

    thanks.

提交回复
热议问题