0

First of all I'm an OG bare metal Java server side guy. That means I'm new to Docker, Node.js (JavaScript in general), socket.io and trying to learn all these things at once so please pardon me if I've made an obvious mistake. I've been working on a pro-bono project for some time in hope that in exchange I get to learn all of these things and move into the new way of doing things.

Now, having said that, moving along to my question - In the code snippet below (especially the last line)

io.on('connection', function (socket) {
  console.log(`connection made from ${socket.id}`);

  socket.on('disconnect', function () {
    console.log(`connection ${socket.id} closed`);
  });

  socket.on(DOWNSTREAM_MESSAGE, function (msg) {
    //console.log(`Received socket payload:  ${msg} from ${socket.id}`);
    iopubClient.publish(DOWNSTREAM_MESSAGE, msg);
  });

  iosubClient.subscribe(UPSTREAM_MESSAGE, message => {
    //console.log(`Sending socket payload: ${message} to ${socket.id}`);
    socket.emit(UPSTREAM_MESSAGE, message);
  });
});

as far as I understand from all the internet sleuthing is that:

io.emit(....

broadcasts to all clients the server socket is connected to, and

socket.emit(....

sends to the call initiator (specifically referring to emit cheat sheet among other sources).

However, with the code snippet above, even when I'm using socket.emit, all subscribing socket.io clients receive the message as if it were a broadcast which isn't what I want. I really do want to send the response back to the io that initiated this particular request.

I've tried almost every way of achieving session stickiness from session stickiness (current implementation an attempt via cookie based stickiness with a HAProxy in front of the service).

In case it helps you help me, full code-base is here.

As I mentioned, I'm learning all of this on the fly so I will not be embarrassed (in fact would be quite thankful!) if you point me to something I'm doing wrong, but for the life of me, I cannot get the response to go only to the request initiator.

Please help?!

  • 2
    when each client connects your then creating the same event `iosubClient.subscribe`, so when you publish, it will fire to all connected clients regardless of which client triggered iosubClient.publish, because they all do. – Lawrence Cherone Aug 07 '22 at 22:14
  • A given `socket.emit()` only goes to one client. But, as Lawrence's comment says, you're doing the same logic for every client and thus you end up doing separate `socket.emit()` calls for every single socket so you end up sending the same message individually to every client. – jfriend00 Aug 07 '22 at 22:41
  • Thanks for your comments! So iosubClient is actually the Redis client which I'm using as the intermediary pub-sub mechanism, and I'm fine with those (Redis) clients receiving Redis subscribed message. However, when I do get the Redis message, I want to pass that along to the socket that initiated the request. How would I trigger a different Redis event for each connected client? I hope that makes sense? Maybe I'm missing something? – Aloke Bhatnagar Aug 08 '22 at 17:49
  • Ahhh... I think I see what you folks are saying... going to try and use redis.psubscribe to publish messages with the socket id included in the channel... – Aloke Bhatnagar Aug 08 '22 at 18:31

0 Answers0