socket.io-redis-adapter icon indicating copy to clipboard operation
socket.io-redis-adapter copied to clipboard

Retrieving number of connected sockets using `fetchSockets` without getting all socket information

Open MatanYemini opened this issue 1 year ago • 5 comments

Hi,

We will need to add a function with the functionality mentioned in the title

(Retrieving number of connected sockets using fetchSockets without getting all socket information)

Currently, in high loaded systems, we have too much traffic in redis since each handshake of a socket contains a lot of information (all the headers, the session info, etc)

Thanks.

I will try to push a PR soon.

MatanYemini avatar Oct 11 '23 19:10 MatanYemini

Hi! Yes, as you have noted the fetchSockets() method transfers a lot of data.

You should use the serverSideEmit() method instead:

io.serverSideEmit("hello", "world");

// And on the receiving side:
io.on("hello", (arg1) => {
  console.log(arg1); // prints "world"
});

Reference: https://socket.io/docs/v4/server-instance/#serversideemit

darrachequesne avatar Oct 13 '23 12:10 darrachequesne

Yeah. I have used that for now.

@darrachequesne What do you think about adding the "count" as a separate function? You don't think it would be usable?

I have also an example of a patch with a new function.

MatanYemini avatar Oct 23 '23 12:10 MatanYemini

What do you think about adding the "count" as a separate function?

I'm not sure that's a good idea, because in most cases I think one wants to count the number of users, not the number of sockets (since a single user can have multiple connections). What do you think?

darrachequesne avatar Oct 23 '23 19:10 darrachequesne

I second this. I do not care if socket count is not always 1 to 1 with user count. I need a way to count sockets without pulling all data over the wire.

cody-evaluate avatar Oct 24 '23 16:10 cody-evaluate

@darrachequesne - Sometimes I actually more interested in the sockets as opposed to the users as @cody-evaluate mentioned.

An example (my case) for that is for horizontal scaling purposes

MatanYemini avatar Oct 27 '23 08:10 MatanYemini