Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
Active reading [<https://en.wikipedia.org/wiki/Socket.IO> <https://en.wiktionary.org/wiki/deprecate#Verb>].
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

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

As of socket.io 1.5, note the change from indexOf which appears to de depreciated, and replaced by valueOf

function findClientsSocket(roomId, namespace) { var res = []; var ns = io.of(namespace ||"/"); // the 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 socket.io 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; } 

As of Socket.IO 1.5, note the change from indexOf which appears to de deprecated, and replaced by valueOf

function findClientsSocket(roomId, namespace) { var res = []; var ns = io.of(namespace ||"/"); // The 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 Socket.IO 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; } 
valueOf() function returns the entire array, which is not what is intended here! We need key:? for the value:roomId. In the revised code, we are fetching all the values from ns.connected[id].rooms object in an array 'rooms' and searching through that array for the value 'roomId'.
Source Link

 

function findClientsSocket(roomId, namespace) { functionvar findClientsSocketres = []; var ns = io.of(roomId,namespace ||"/"); // the default namespace is "/" if (ns) {   for (var resid 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 socket.io 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) { //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; } 

 

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

 

 

 function findClientsSocket(roomId, namespace) { var res = []; var ns = io.of(namespace ||"/"); // the 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; } 

 

 
function findClientsSocket(roomId, namespace) { var res = []; var ns = io.of(namespace ||"/"); // the 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 socket.io 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; } 
Source Link
MoleIsKing
  • 131
  • 1
  • 5

As of socket.io 1.5, note the change from indexOf which appears to de depreciated, and replaced by valueOf

 function findClientsSocket(roomId, namespace) { var res = []; var ns = io.of(namespace ||"/"); // the 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; }