Socket.io: Not able send message to particular socket using socket.id

痞子三分冷 提交于 2019-12-25 00:42:40

问题


I am storing the socket.id of each user when he login using redis

redisClient.set(user._id + ':socket', socket.id)

When any post for this user._id is created, i am trying emit an event to the socket of that particular user using the socket.id stored above

var mySocket=redisClient.get(user._id + ':socket')

socket.broadcast.to(mySocket).emit('my message', msg);

The problem is that I am not able to emit to that particular socket using the above code.I am using socket.io version > 1.0. Can someone help me to understand if am getting the socket.id wrong way


回答1:


Give this a try;

  io.to(socket.id).emit("event", data);

It Should help you..!!



来源:https://stackoverflow.com/questions/29069658/socket-io-not-able-send-message-to-particular-socket-using-socket-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!