5

I am following the tutorial at this link (step 7). I successfully installed both node.js either socket.js , but when i go to the page and try to send the image i get this error on the server

Missing error handler on `socket`.
TypeError: io.sockets.clients is not a function
at Socket.<anonymous> (C:\Users\utente\Projects\webrtc\server.js:30:31)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at Socket.onevent (C:\Users\utente\Projects\webrtc\node_modules\socket.io\lib\socket.js:330:8)
at Socket.onpacket (C:\Users\utente\Projects\webrtc\node_modules\socket.io\lib\socket.js:290:12)
at Client.ondecoded (C:\Users\utente\Projects\webrtc\node_modules\socket.io\lib\client.js:193:14)
at Decoder.Emitter.emit (C:\Users\utente\Projects\webrtc\node_modules\socket.io\node_modules\socket.io-parser\node_modules\component-emitter\index.js:134:20)
at Decoder.add (C:\Users\utente\Projects\webrtc\node_modules\socket.io\node_modules\socket.io-parser\index.js:247:12)
at Client.ondata (C:\Users\utente\Projects\webrtc\node_modules\socket.io\lib\client.js:175:18)
at emitOne (events.js:77:13)

my server.js file is

var os = require('os');
var static = require('node-static');
var http = require('http');
var socketIO = require('socket.io');

var fileServer = new(static.Server)();
var app = http.createServer(function (req, res) {
fileServer.serve(req, res);
}).listen(2013);

var io = socketIO.listen(app);
io.sockets.on('connection', function (socket){

// convenience function to log server messages on the client
function log(){
    var array = [">>> Message from server:"];
    array.push.apply(array, arguments);
    socket.emit('log', array);
}

socket.on('message', function (message) {
    log('Client said:', message);
    // for a real app, would be room only (not broadcast)
    socket.broadcast.emit('message', message);
});

socket.on('create or join', function (room) {
    log('Request to create or join room ' + room);

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

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

    } else if (numClients === 1) {
        socket.join(room);
        socket.emit('joined', room, socket.id);
        io.sockets.in(room).emit('ready');

    } else { // max two clients
        socket.emit('full', room);
    }
});

socket.on('ipaddr', function () {
    var ifaces = os.networkInterfaces();
    for (var dev in ifaces) {
        ifaces[dev].forEach(function (details) {
            if (details.family=='IPv4' && details.address != '127.0.0.1') {
                socket.emit('ipaddr', details.address);
            }
      });
    }
   });

});

the index.html is

<!DOCTYPE html>
<html>
<head>
    <meta name="keywords" content="JavaScript, WebRTC" />
    <meta name="description" content="WebRTC codelab" />
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1">

    <title>WebRTC codelab: step X</title>
    <link rel="stylesheet" href="css/main.css">
</head>
<body>
    <h1>WebRTC codelab: step X</h1>

    <p>
        Room URL: <br>
        <code id="url">...</code>
    </p>

    <video class="camera" autoplay></video>
    <div class="controls">
        <canvas id="photo" style="width: 200px; height: 150px; border: 1px solid #ccc;"></canvas>
        <p>
            <button id="snap">snap</button>-then-<button id="send">send</button> 
            <br> - or - <br>
            <button id="snapAndSend">snap & send</button>
        </p>
    </div>

    <div class="incoming">
        <h2>Incoming photos</h2>
        <div id="trail"></div>
    </div>

    <script src="node_modules\socket.io\node_modules\socket.io-client\socket.io.js"></script>
    <script src="js/lib/adapter.js"></script>
    <script src="js/main.js"></script>

</body>

any help?

thanks a lot

with io.sockets.clients[room].length; i solved the error but now i get

Missing error handler on `socket`.
TypeError: Cannot read property '8a675bfe1203e' of undefined
at Socket.<anonymous> (C:\Users\utente\Projects\webrtc\server.js:30:38)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at Socket.onevent (C:\Users\utente\Projects\webrtc\node_modules\socket.io\lib\socket.js:330:8)
at Socket.onpacket (C:\Users\utente\Projects\webrtc\node_modules\socket.io\lib\socket.js:290:12)
at Client.ondecoded (C:\Users\utente\Projects\webrtc\node_modules\socket.io\lib\client.js:193:14)
at Decoder.Emitter.emit (C:\Users\utente\Projects\webrtc\node_modules\socket.io\node_modules\socket.io-parser\node_modules\component-emitter\index.js:134:20)
at Decoder.add (C:\Users\utente\Projects\webrtc\node_modules\socket.io\node_modules\socket.io-parser\index.js:247:12)
at Client.ondata (C:\Users\utente\Projects\webrtc\node_modules\socket.io\lib\client.js:175:18)
at emitOne (events.js:77:13)
elgeko
  • 127
  • 1
  • 1
  • 11
  • Possible duplicate of [How to get room's clients list in socket.io 1.0](http://stackoverflow.com/questions/23858604/how-to-get-rooms-clients-list-in-socket-io-1-0) – Vishnu Oct 27 '15 at 16:24

4 Answers4

5

From what the log says the error is thrown at line 30: var numClients = io.sockets.clients(room).length;

The reason is that the method io.sockets.clients(room); does not longer work on socket.io v1.0+

You can obtain the list of clients by calling:

var clientsList = io.sockets.adapter.rooms[room];
var numClients = clientsList.length;
Gary Torres
  • 520
  • 7
  • 18
  • with io.sockets.clients[room].length; i solved the error but now i get Missing error handler on `socket`. TypeError: Cannot read property '8a675bfe1203e' of undefined at Socket. (C:\Users\utente\Projects\webrtc\server.js:30:38) – elgeko Oct 28 '15 at 08:24
  • Oh wow, this post has remained open for a long time. Hey @elgeko since my recommendation helped you fix the error from your original question, would you mind selecting it as the answer for the problem. That would help me and other devs a lot! Thanks! – Gary Torres Nov 27 '20 at 20:10
1

The issue appears if you update the dependencies to the latest versions:

"dependencies": {
    "mime": "^1.3.4",
    "socket.io": "^1.4.5"
}

while it works if you use the dependencies specified by the book

"dependencies": {
   "socket.io": "~0.9.6",
    "mime": "~1.2.7"
}

I checked the example in the book NodeJs in action, and of course it will not work if you use the latest versions of socket.io, you will have the 2 following lines to get it to work:

From:

var usersInRoom = io.sockets.clients(room);

To:

var usersInRoom = io.of('/').in(room).clients;

and from:

socket.emit('rooms', io.sockets.manager.rooms);

to:

socket.emit('rooms', io.of('/').adapter.rooms);

Greetings!

Maximilian Ast
  • 3,369
  • 12
  • 36
  • 47
0

You can just the change of line of code to

var numClients = io.sockets.adapter.rooms[room]!=undefined ? Object.keys(io.sockets.adapter.rooms[room]).length:0;
bluej
  • 1
  • 1
0

To get the number of clients or sockets connected to a particular room you can try this:

// io.of(<namespace>).adapter.rooms  gives a Map containing all rooms in
// the <namespace>, So get() method is used to get all the clients joint  
// to the room <roomName>
const clientList = io.of("<namespace>").adapter.rooms.get(<roomName>);
// You must provide a namespace

// clientList is a set
const numOfClients = clientList.size;
Tyler2P
  • 2,324
  • 26
  • 22
  • 31