Rajkamal Subramanian
Rajkamal Subramanian

Reputation: 6944

getConnections method of node.js tcp server not defined

var server = net.createServer(function(c) {
    ...
})
server.getConnections(function(err, count){
    console.log("count", count);
})

I get the following error.

Object #<Server> has no method 'getConnections'
  1. How to use getConnections of a tcp server?

I'm using node version v0.10.16

Upvotes: 0

Views: 4301

Answers (2)

Catalin
Catalin

Reputation: 1

You can try like this:

var net = require('net');
var events = require('events');
var eventEmitter = new events.EventEmitter();

var server = net.createServer(function(c) {
    ...`enter code here`
eventEmitter.emit('event1');

})


eventEmitter.on('event1', function(){
   server.getConnections(function(err, count){
        console.log("count", count);
    })
});

Upvotes: 0

WiredPrairie
WiredPrairie

Reputation: 59763

I'm not sure exactly why your code doesn't work. While having the request to getConnections outside of the connection callback is not necessary typical, it worked in my tests. server.connections is deprecated per the documentation, so it's use is discouraged.

Using telnet localhost 1337, a really poor echo socket is emulated below, and the current count of connections is displayed. The code below worked in my tests:

var server = require('net').createServer();

server.on('connection', function (socket) {

    socket.on('data', function(data) {
       socket.write(data);
    });
    server.getConnections(function(err, count) {
       console.log("Connections: " + count);
    });
});
server.listen(1337);

Upvotes: 1

Related Questions