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

前端 未结 6 1991
無奈伤痛
無奈伤痛 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条回答
  •  -上瘾入骨i
    2020-11-28 00:53

    In a project of our company we are using "rooms" approach and it's name is a combination of user ids of all users in a conversation as a unique identifier (our implementation is more like facebook messenger), example:

    |id | name |1 | Scott |2 | Susan

    "room" name will be "1-2" (ids are ordered Asc.) and on disconnect socket.io automatically cleans up the room

    this way you send messages just to that room and only to online (connected) users (less packages sent throughout the server).

提交回复
热议问题