Ws: Get total number of connected clients on each new connection

Created on 25 Apr 2017  路  4Comments  路  Source: websockets/ws

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.

Most helpful comment

wss.clients is a set not an array. Use wss.clients.size.

All 4 comments

wss.clients is a set not an array. Use wss.clients.size.

Ahhhhhhhhh. That worked.

it always returns 1.

wss.clients.size thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cmnstmntmn picture cmnstmntmn  路  4Comments

quesurifn picture quesurifn  路  3Comments

dcflow picture dcflow  路  4Comments

nabeelio picture nabeelio  路  5Comments

pacmac picture pacmac  路  3Comments