Wednesday 3 October 2018

EventEmitter.defaultMaxListeners

‘EventEmitter.defaultMaxListeners’ specifies the default maximum listeners for an emitter instance for any single event. As per the node.js documentation, maximum default listeners are 10.

HelloWorld.js
var EventEmitter = require('events').EventEmitter;

var emitter = new EventEmitter();

console.log(`Maximum Default listeners : ${EventEmitter.defaultMaxListeners}`);
console.log(`Maximum default listeners for an emitter instance : ${emitter.getMaxListeners()}`);

Output
Maximum Default listeners : 10
Maximum default listeners for an emitter instance : 10

How to change the default maximum listeners value for all the emitters?
Use the property ‘EventEmitter.defaultMaxListeners’ to change the default maximum listeners.

HelloWorld.js
var EventEmitter = require('events').EventEmitter;

var emitter = new EventEmitter();

/* Setting the default maximum listeners to 20 */
EventEmitter.defaultMaxListeners = 20;

console.log(`Maximum Default listeners : ${EventEmitter.defaultMaxListeners}`);
console.log(`Maximum default listeners for an emitter instance : ${emitter.getMaxListeners()}`);

Output
Maximum Default listeners : 20
Maximum default listeners for an emitter instance : 20

What is the behavior if total number of listeners for an event greater than EventEmitter.defaultMaxListeners?
node.js throws a warning like ‘MaxListenersExceededWarning: Possible EventEmitter memory leak detected. n demo listeners added. Use emitter.setMaxListeners() to increase limit’.

Let us confirm the same with demo application.


HelloWorld.js

var EventEmitter = require('events').EventEmitter;

var emitter = new EventEmitter();

emitter.on('demo',() => {
 console.log("listener1");
});

emitter.on('demo',() => {
 console.log("listener2");
});

emitter.on('demo',() => {
 console.log("listener3");
});

emitter.on('demo',() => {
 console.log("listener4");
});

emitter.on('demo',() => {
 console.log("listener5");
});

emitter.on('demo',() => {
 console.log("listener6");
});

emitter.on('demo',() => {
 console.log("listener7");
});

emitter.on('demo',() => {
 console.log("listener8");
});

emitter.on('demo',() => {
 console.log("listener9");
});

emitter.on('demo',() => {
 console.log("listener10");
});

emitter.on('demo',() => {
 console.log("listener11");
});

emitter.emit('demo')


Output
listener1
listener2
listener3
listener4
listener5
listener6
listener7
listener8
listener9
listener10
listener11
(node:18604) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 demo listeners added. Use emitter.setMaxListeners() to increase limit

How to set the maximum listeners for specific emitter instance?
‘emitter.setMaxListeners()´ method is used to increase (or) decrease the maximum listeners limit. Calling emitter.setMaxListeners(n) takes precedence over EventEmitter.defaultMaxListeners.


HelloWorld.js

var EventEmitter = require('events').EventEmitter;

var emitter1 = new EventEmitter();
var emitter2 = new EventEmitter();

/* Set the maximum listeners to 29*/
emitter1.setMaxListeners(29);

console.log("Maximum listeners for emitter1 : %d", emitter1.getMaxListeners());
console.log("Maximum listeners for emitter2 : %d", emitter2.getMaxListeners());
console.log("Default Maximum listeners for all emitter instances : %d", EventEmitter.defaultMaxListeners);

Output
Maximum listeners for emitter1 : 29
Maximum listeners for emitter2 : 10
Default Maximum listeners for all emitter instances : 10

You can inspect the emitted warnings from a process using ‘proess.on('warning')’.

HelloWorld.js

var EventEmitter = require('events').EventEmitter;

EventEmitter.defaultMaxListeners = 3;

var emitter = new EventEmitter();

emitter.on('demo',() => {
 console.log("listener1");
});

emitter.on('demo',() => {
 console.log("listener2");
});

emitter.on('demo',() => {
 console.log("listener3");
});

emitter.on('demo',() => {
 console.log("listener4");
});

emitter.emit('demo');

process.on('warning', (warning) => {
  console.warn("Warning name : ", warning.name);    // Print the warning name
  console.warn("Warning message : ", warning.message); // Print the warning message
  console.warn("Warning stack trace : ", warning.stack);   // Print the stack trace
});


Output
listener1
listener2
listener3
listener4
(node:27464) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 4 demo listeners added. Use emitter.setMaxListeners() to increase limit
Warning name :  MaxListenersExceededWarning
Warning message :  Possible EventEmitter memory leak detected. 4 demo listeners added. Use emitter.setMaxListeners() to increase limit
Warning stack trace :  MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 4 demo listeners added. Use emitter.setMaxListeners() to increase limit
    at _addListener (events.js:243:17)
    at EventEmitter.addListener (events.js:259:10)
    at Object.<anonymous> (C:\Users\krishna\Documents\Study\nodejs\examples\HelloWorld.js:19:9)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:266:19)



Previous                                                 Next                                                 Home

No comments:

Post a Comment