4

I have multiple processes & servers, and am using socket.io-redis with session affinity (on Heroku).

When a client connects, I store a map of the client's user ID to the client's socket ID in Redis. Upon disconnecting, I delete the client's user ID from Redis.

I would like to be able to push a message to a client by looking up the client's socket ID, then sending a message to the specific socket ID.

One way to do this is io.to(socketId).emit(...). However, this does not allow me to send a callback with the message, which I would really like because I want to have confirmation of message receipt.

Another way I have found is io.connected[socketId].emit(...). The problem with this is that it does not scale to multiple servers, since the io.connected object may not necessarily be the same on every server since it only records sockets that the server itself is connected to. This solution does allow callbacks, however.

Is there a way to solve this so I can emit messages to a specific socket ID from any server or process, and also send a callback with the message?

Ismail Khan
  • 842
  • 2
  • 8
  • 20
  • One of the constraints Socket.io, SockJS and similar libraries have is that they need to continuously talk to the same instance of the server. They work perfectly well when there is only 1 instance of the server. – Asif Saeed Jan 21 '17 at 18:34
  • check this repo description this might help you https://github.com/asyf/redispubsub – Asif Saeed Jan 21 '17 at 18:35
  • 1
    Yes, this is why I am using the socket.io-redis adapter. But it doesn't answer my question! – Ismail Khan Jan 21 '17 at 21:02
  • This is a specific need to your project, and is not the responsibility for socket.io to come up this feature, because it's difficult to share functions across nodes/machines (all communication are serialized into plain text). But you can imitate this functionality easily with a few extra lines of code on both server and client sides and use Redis Pub Sub as communication bridge between the nodes. – Maria Jun 06 '18 at 04:52

0 Answers0