Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You'll need to obtain the number of clients from a different location:</p> <pre><code> io.sockets.clients().length </code></pre> <p>However, when you read <a href="https://github.com/LearnBoost/socket.io/issues/463">this active GitHub issue</a>, you might want to avoid that operation as it can cause an additional memory leak (beyond what's already happening with sockets).</p> <p>Here's a complete working example:</p> <pre><code>var app = require('express')(); var server = require("http").createServer(app); var io = require('socket.io').listen(server); server.listen(1339); app.get('/', function(req, res) { return res.sendfile(__dirname + "/index.html"); }); /* to obtain number of connected clients io.sockets.clients().length */ console.log("INIT", io.sockets.clients().length ); io.sockets.on("connection", function(socket) { console.log("CONNECT:" + io.sockets.clients().length); socket.on("disconnect", function(data){ console.log("DISCONNECT: ", io.sockets.clients().length); }); }); </code></pre> <p>The disconnect event happens before the client has been removed, so the count will be one higher than the total number of clients (depending on how you look at it).</p> <p>Also, you had the <code>on</code> event handlers returning values for some reason, so I removed that.</p> <p>Instead of using that function, you might have better luck with just a simple counter:</p> <pre><code>var clientsConnected = 0; console.log("INIT", io.sockets.clients().length ); io.sockets.on("connection", function(socket) { console.log("CONNECT:" + ++clientsConnected); socket.on("disconnect", function(data){ console.log("DISCONNECT: ", --clientsConnected); }); }); </code></pre> <p>Some have apparently reported that the number of disconnects can exceed the number of connections, so you may want to prevent a counter from dropping below zero. </p> <p>Connected two clients (and then disconnected one):</p> <pre><code>info: socket.io started INIT 0 debug: served static content /socket.io.js debug: client authorized info: handshake authorized eJcvVsr60fmRzhVpQ6rO debug: setting request GET /socket.io/1/websocket/eJcvVsr60fmRzhVpQ6rO debug: set heartbeat interval for client eJcvVsr60fmRzhVpQ6rO debug: client authorized for debug: websocket writing 1:: CONNECT:1 debug: served static content /socket.io.js debug: client authorized info: handshake authorized 28lUudTS3KtCphd0Q6rP debug: setting request GET /socket.io/1/websocket/28lUudTS3KtCphd0Q6rP debug: set heartbeat interval for client 28lUudTS3KtCphd0Q6rP debug: client authorized for debug: websocket writing 1:: CONNECT:2 info: transport end (socket end) debug: set close timeout for client 28lUudTS3KtCphd0Q6rP debug: cleared close timeout for client 28lUudTS3KtCphd0Q6rP debug: cleared heartbeat interval for client 28lUudTS3KtCphd0Q6rP DISCONNECT: 2 debug: discarding transport info: transport end (socket end) debug: set close timeout for client eJcvVsr60fmRzhVpQ6rO debug: cleared close timeout for client eJcvVsr60fmRzhVpQ6rO debug: cleared heartbeat interval for client eJcvVsr60fmRzhVpQ6rO DISCONNECT: 1 debug: discarding transport </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload