Limit the client connections on Socket IO

Viewed 87

I am attempting to limit the number of clients connected to my socket.io server using nodeJS, I manage to count the number of connections on my server by pushing the socket into a connection array.

So the plan is to compare the connection array length into another variable (limit) and the server will not allow the incoming connection, now I was wondering if there's a built in function in socket.io to disallow this.

Here is my code.

  var server = require("http").createServer(app);
  var io = require("socket.io").listen(server);
  var limit = 3;
  var connections = [];




 io.sockets.on("connection", function(socket) {
      connections.push(socket);
      console.log('total connection : ' + connections.length);
       if(connections.length > limit){
         console.log('disallow connection');
       }
 });
0 Answers
Related