I am trying to print the number of connected clients each time a connection is established.
Here is the server code:
const WebSocket = require('ws');
const wss = new WebSocket.Server();
wss.on('connection', (ws) => {
ws.on('message', (m) => {
console.log('received: %s', m);
});
console.log(wss.clients.length);
});
When I connect with a client though I am seeing wss.clients.length as undefined.
wss.clients is a set not an array. Use wss.clients.size.
Ahhhhhhhhh. That worked.
it always returns 1.
wss.clients.size thanks
Most helpful comment
wss.clientsis a set not an array. Usewss.clients.size.