Search code examples
node.jssocket.ioconnectionlimit

Limit Socket io connections number


Is it possible to limit socket io connections number in nodejs ?

I know one can configure limit by changing the server TCP settings, but I am looking for a way to do this in nodejs.


Solution

  • You can implement it very easy:

    var connectionsLimit = 1
    
    io.on('connection', function (socket) {
    
      if (io.engine.clientsCount > connectionsLimit) {
        socket.emit('err', { message: 'reach the limit of connections' })
        socket.disconnect()
        console.log('Disconnected...')
        return
      }
    
    })