Sending message to specific client with socket.io and empty message queue

后端 未结 9 2004
误落风尘
误落风尘 2020-12-22 20:16

I´m going crazy with socket.io! Documentation is so bad it\'s simply not true.

I want to send a feedback to specific client over socket.io

My server side loo

9条回答
  •  温柔的废话
    2020-12-22 20:36

    I believe io.sockets.socket has been removed and has been a problem in Socket.IO (https://github.com/socketio/socket.io/issues/1618).

    You can use io.sockets.connected[socket.id] and store the socket.id to reference with the user via username:

    var usernames = {};
    usernames[username] = socket.id;
    // Sending the message
    io.sockets.connected[usernames[username]].emit(...);
    

    I don't see it anywhere on the documentation so I can see how this hasn't been answered. Also, if you don't have any reference via usernames, you can instead try:

    users[socket.id] = socket.id;
    

    to duplicate the key and value for easy access.

    There is also possibly another way by using io.clients as described below, but I haven't used that method.

    OTHER SOLUTION: Send message to specific client with socket.io and node.js

提交回复
热议问题