socketcluster icon indicating copy to clipboard operation
socketcluster copied to clipboard

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

Open synzhu opened this issue 5 years ago • 2 comments

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?

synzhu avatar Apr 30 '20 05:04 synzhu

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);
	}
})();

ahmetbozdoan avatar Dec 11 '20 07:12 ahmetbozdoan

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.

maarteNNNN avatar Feb 02 '21 14:02 maarteNNNN