Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get total number of connected clients on each new connection #1087

Closed
nodesocket opened this issue Apr 25, 2017 · 5 comments
Closed

Get total number of connected clients on each new connection #1087

nodesocket opened this issue Apr 25, 2017 · 5 comments

Comments

@nodesocket
Copy link

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.

@lpinca
Copy link
Member

lpinca commented Apr 25, 2017

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

@nodesocket
Copy link
Author

Ahhhhhhhhh. That worked.

@amirzandi
Copy link

it always returns 1.

@jeremy16601
Copy link

wss.clients.size thanks

@ajaykumar010
Copy link

ajaykumar010 commented Feb 22, 2023

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

Thats not return Proper connected Number of Client size or lenght

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants