question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Not correctly checking number of clients in room (step-04)

See original GitHub issue

In step-04/index.js the server checks how many clients are in the room by calling

var numClients = io.sockets.sockets.length;
log('Room ' + room + ' now has ' + numClients + ' client(s)');

However this returns undefined, as well as doesn’t actually try and check for the given room.

A proposed fix:

var clientsInRoom = io.nsps['/'].adapter.rooms[room];
var numClients = clientsInRoom === undefined ? 0 : Object.keys(clientsInRoom).length;

Or if you also want to upgrade to socket v1.4

var clientsInRoom = io.nsps['/'].adapter.rooms[room];
var numClients = clientsInRoom === undefined ? 0 : Object.keys(clientsInRoom.sockets).length;

A full fix + some small cleans up 😃 would look like

var clientsInRoom = io.nsps['/'].adapter.rooms[room];
var numClients = clientsInRoom === undefined ? 0 : Object.keys(clientsInRoom).
// socket.io 1.4.8
var numClients = clientsInRoom === undefined ? 0 : Object.keys(clientsInRoom.sockets).length;

// max two clients
if (numClients === 2) {
  socket.emit('full', room);
  return;
}

log('Room ' + room + ' now has ' + (numClients + 1) + ' client(s)');

if (numClients === 0) {
  socket.join(room);
  log('Client ID ' + socket.id + ' created room ' + room);
  socket.emit('created', room, socket.id);

} else {
  log('Client ID ' + socket.id + ' joined room ' + room);
  io.sockets.in(room).emit('join', room);
  socket.join(room);
  socket.emit('joined', room, socket.id);
  io.sockets.in(room).emit('ready');
}

See: https://github.com/AlexRobinson-/webrtc-web/blob/fix-check-room-clients/step-04/index.js

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:4
  • Comments:21 (17 by maintainers)

github_iconTop GitHub Comments

3reactions
samduttoncommented, Feb 14, 2017

@aaronang go for it!

1reaction
samduttoncommented, Feb 21, 2018

@thbaja @nitobuendia Thank you!

I’ve published the changes to codelab text.

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to get socket.io number of clients in room? - Stack Overflow
my socket.io version 1.3.5​​ on('create or join', function (numClients, room) { socket. join(room); }); I use this code for get clients in room...
Read more >
discord python bot examples - Fenice S.r.l
Im going to be hosting a discord bot 24/7 and i want the Status to display the current date and time, as example....
Read more >
Hdwificampro instructions - Edizioni La Grafica
Step 04 : Search by HDWifiCamPro from the Tools section and Click on the “Install” button. Hardware controls. 7V Playback software You are...
Read more >
bypass google drive limit reddit
Bypass Google Drive Download Limit (Quota Exceeded) Error 2021 [New Method]. There's a few steps we need to go through to accomplish this:...
Read more >
Getting Started with Translational Core
You will need a valid payment method (Lawson account number or PO#) associated with ... Step 04. Submit samples. All samples must be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found