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

1

0

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

Constituency answered 16/3, 2015 at 4:29 Comment(1)
are you sure that you are getting correct "user._id" while retrieving it from redis ?Formality
F
1

Give this a try;

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

It Should help you..!!

Formality answered 16/3, 2015 at 5:18 Comment(1)
Thanks for the reply Mahida, But I tried this and didnt work. I think the socket.id is changing and I am not storing the latest one for that user.Constituency

© 2022 - 2024 — McMap. All rights reserved.