SocketCluster / socketcluster

Highly scalable realtime pub/sub and RPC framework
https://socketcluster.io
MIT License
6.15k stars 314 forks source link

Any way to see list of clients subscribed to a channel? #528

Open synzhu opened 4 years ago

synzhu commented 4 years ago

Hey guys, so I've seen from the documentation that it's possible to get the list of channels that a client is subscribed to. Is there any way to see a list of clients ID's (across all nodes / machines) that are subscribed to a particular channel?

ahmetbozdoan commented 3 years ago

You can create your own list by adding the following code to server.js

let subscriptionList = [];

(async () => {
    for await (let {socket, channel} of agServer.listener('subscription')) {
        subscriptionList.push(channel);

        console.log("subscriptionList: ",subscriptionList);
    }
})();

(async () => {
    for await (let {socket, channel} of agServer.listener('unsubscription')) {
        const index = subscriptionList.indexOf(channel);
        if (index > -1) {
            subscriptionList.splice(index, 1);
        }

        console.log("subscriptionList: ",subscriptionList);
    }
})();
maarteNNNN commented 3 years ago

There is a way to do that.

for (let i = 0; i < Object.keys(AGServer.clients).length; i++) {
  const clientId = Object.keys(AGServer.clients)[i]; // id of the socket
  // This will return an object of channels eg { testChannel: true }, which means the client is subscribed to testChannel and cleared if the client unsubscribes again
  console.log(AGServer.clients[clientId].channelSubscriptions)
}

However I didn't test this across node / machines but I presume it should work.