Is it possible to check whether a user has subscribed to a certain channel in socket.io?
Let´s say I have a channel called news
. User subscribed to that channel on client-side. But the site´s data is dynamic, therefore the news-tab of the site might not be open at any time. I do not want to create the content for the news-tab if the news tab is not open on the client-side. I know that the news-tab is not open, when the user has not subscribed to the news
channel.
Is there a way to check that?
You can us io.sockets.clients('news')
and this will return the socket id's of all clients. If you know the client socket.id, you could also call io.sockets.manager.roomClients[socket.id]
(room name will have '/' leading character)
This is a sample I use for my admin clients to call to get client counts per room:
socket.on('countClientsInRoom', function (room, fn) {
var cnt = io.sockets.clients(room).length;
fn(cnt);
});