5

I read some other questions and posts, but I couldn't find where to apply .setMaxListeners(0). I'm using a simple websocket-server which comes up with the errer:

(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.
Trace
at Socket.EventEmitter.addListener (events.js:160:15)
at Socket.Readable.on (_stream_readable.js:689:33)
at XHRPolling.Transport.setHandlers            (D:\nodeJS\host\node_modules\socket.io\lib\transport.js:116:15)
at XHRPolling.HTTPPolling.setHandlers        (D:\nodeJS\host\node_modules\socket.io\lib\transports\http-polling.js:53:39)
at XHRPolling.Transport.handleRequest (D:\nodeJS\host\node_modules\socket.io\lib\transport.js:70:10)
at XHRPolling.HTTPTransport.handleRequest (D:\nodeJS\host\node_modules\socket.io\lib\transports\http.js:84:39)
at XHRPolling.HTTPPolling.handleRequest (D:\nodeJS\host\node_modules\socket.io\lib\transports\http-polling.js:73:41)
at XHRPolling.Transport (D:\nodeJS\host\node_modules\socket.io\lib\transport.js:31:8)
at XHRPolling.HTTPTransport (D:\nodeJS\host\node_modules\socket.io\lib\transports\http.js:29:13)
at XHRPolling.HTTPPolling (D:\nodeJS\host\node_modules\socket.io\lib\transports\http-polling.js:27:17)

I'm instantiating the router this way:

var app = require('http').createServer(handler)
, io = require('socket.io').listen(app, { log: false })
, fs = require('fs');
app.listen(8070);

Then I'm listening for incomming socket-requests:

io.sockets.on('connection', function (socket) {

       socket.on('createTake', function(data){...}
});

Where can I apply the MaxListeners?

I already tried this:

io.sockets.on(...).setMaxListeners(0);

But it doesn't work.

marcel
  • 3,231
  • 8
  • 43
  • 76

2 Answers2

10

You are attaching listeners to io.sockets (io.sockets.on(...)). And you should apply setMaxListeners to the same object (to EventEmmiter). So try:

io.sockets.setMaxListeners(0);
tomekK
  • 748
  • 8
  • 19
  • tyvm. I just couldn't imagine where to set it. Now it works :) – marcel Nov 19 '13 at 08:42
  • You welcome. But don't ignore this warning easly. Check if you shouldn't remove listeners somewhere in your code when they are not needed any more. And if you need to react only once for event use io.sockets.once(...) – tomekK Nov 19 '13 at 09:02
4

This is regulated by "maxListeners" option. You have 2 ways: disable memory leak completely (i would not recommend doing this) or increase maxListeners.

1. To keep feature active, just not too aggressive:

Default value is 10, increase this value instead to keep memory leak detection active, but not so aggressive. Enter whatever number you need. I would start with little steps, if you don't know actually how many listeners you will be using. In error you got number how much listeners you attached at that moment.

io.sockets.setMaxListeners(15);

2. Disable memory leak feature completely:

io.sockets.setMaxListeners(0);

Other connected tips:

Get current value:

io.sockets.getMaxListeners();

If you need to react only once for event use io.sockets.once(...) it will remove unused listeners. tomekK

Community
  • 1
  • 1
Lukas Liesis
  • 24,652
  • 10
  • 111
  • 109
  • Is there a event I can listen to so that i can know when this max listener reached. – Santhosh S Kashyap Sep 07 '17 at 12:26
  • you can wait for memory leak error message `warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit. Trace` and just handle that how you want. – Lukas Liesis Sep 08 '17 at 04:55
  • What event should i listen for – Santhosh S Kashyap Sep 08 '17 at 09:06
  • @SanthoshSKashyap none. Just catch errors and parse the message if that's not the error you are waiting for, throw it again. This might help: https://stackoverflow.com/questions/7310521/node-js-best-practice-exception-handling – Lukas Liesis Sep 09 '17 at 18:29