As of socketSocket.ioIO 1.5, note the change from indexOfindexOf which appears to de depreciateddeprecated, and replaced by valueOfvalueOf
function findClientsSocket(roomId, namespace) { var res = []; var ns = io.of(namespace ||"/"); // theThe default namespace is "/" if (ns) { for (var id in ns.connected) { if (roomId) { //var index = ns.connected[id].rooms.indexOf(roomId) ; var index = ns.connected[id].rooms.valueOf(roomId) ; //Problem was here if(index !== -1) { res.push(ns.connected[id]); } } else { res.push(ns.connected[id]); } } } return res.length; } For socketSocket.ioIO version 2.0.3, the following code works:
function findClientsSocket(io, roomId, namespace) { var res = [], ns = io.of(namespace ||"/"); // the default namespace is "/" if (ns) { for (var id in ns.connected) { if(roomId) { // ns.connected[id].rooms is an object! var rooms = Object.values(ns.connected[id].rooms); var index = rooms.indexOf(roomId); if(index !== -1) { res.push(ns.connected[id]); } } else { res.push(ns.connected[id]); } } } return res; }