Node.js sends warnings when you add too many listeners to an event emitter (#tilPost)

Today I was reading the documentation of events in Node.js and discovered something interesting.
When you use them you usually also use an EventEmitter. Let’s have a quick look at an example snippet from the docs.
const EventEmitter…


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

Today I was reading the documentation of events in Node.js and discovered something interesting.

When you use them you usually also use an EventEmitter. Let's have a quick look at an example snippet from the docs.

const EventEmitter = require('events');

class MyEmitter extends EventEmitter {}

const myEmitter = new MyEmitter();
myEmitter.on('event', () => {
  console.log('an event occurred!');
});
myEmitter.emit('event');

The usage is straightforward. Create an emitter, emit events and react to them. Let's change the code above and add a few more event handlers.

const EventEmitter = require('events');

class MyEmitter extends EventEmitter {}

const myEmitter = new MyEmitter();
for(let i = 0; i < 11; i++) {
  myEmitter.on('event', _ => console.log(i));
}

myEmitter.emit('event');

And execute it.

$ node index.js
0
1
2
3
4
5
6
7
8
9
10
(node:10031) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 event listeners added. Use emitter.setMaxListeners() to increase limit

Interesting, Node.js sends a warning to stderr when you add more than ten listeners for one specific event to an event emitter. Having 30 listeners reacting to 30 different events is fine, though. If you want to dig a bit deeper, you can find the warning in the Node.js source code.

This warning helps you to prevent memory leaks. Node.js processes can run for ages and when you have a bug in your code and create a new event listener before cleaning up, or you don't use existing ones the memory usage of this process will slowly grow and make troubles on your servers at some point.

It has to be pointed out that this is "just" a warning and the Node.js process will still execute the eleven added listeners. It won't terminate the process, will only appear once per event and it is more about pointing out problems in your source code.

Sometimes you do need more than ten listeners for an event on an event emitter, though. This is the situation, where setMaxListeners comes into play. A function that is also used several times in the Node.js project itself.

const EventEmitter = require('events');

class MyEmitter extends EventEmitter {}

const myEmitter = new MyEmitter();
// increase the limit
myEmitter.setMaxListeners(11);

for(let i = 0; i < 11; i++) {
  myEmitter.on('event', _ => console.log(i));
}

myEmitter.emit('event');

Using setMaxListeners way you can quickly get rid of warnings regarding the number of listeners and go on with coding. I'd say this warning is a pretty good example of good developer experience. ??


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate
APA
Stefan Judis | Sciencx (2022-12-08T16:41:45+00:00) » Node.js sends warnings when you add too many listeners to an event emitter (#tilPost). Retrieved from https://www.scien.cx/2018/02/08/node-js-sends-warnings-when-you-add-too-many-listeners-to-an-event-emitter-tilpost/.
MLA
" » Node.js sends warnings when you add too many listeners to an event emitter (#tilPost)." Stefan Judis | Sciencx - Thursday February 8, 2018, https://www.scien.cx/2018/02/08/node-js-sends-warnings-when-you-add-too-many-listeners-to-an-event-emitter-tilpost/
HARVARD
Stefan Judis | Sciencx Thursday February 8, 2018 » Node.js sends warnings when you add too many listeners to an event emitter (#tilPost)., viewed 2022-12-08T16:41:45+00:00,<https://www.scien.cx/2018/02/08/node-js-sends-warnings-when-you-add-too-many-listeners-to-an-event-emitter-tilpost/>
VANCOUVER
Stefan Judis | Sciencx - » Node.js sends warnings when you add too many listeners to an event emitter (#tilPost). [Internet]. [Accessed 2022-12-08T16:41:45+00:00]. Available from: https://www.scien.cx/2018/02/08/node-js-sends-warnings-when-you-add-too-many-listeners-to-an-event-emitter-tilpost/
CHICAGO
" » Node.js sends warnings when you add too many listeners to an event emitter (#tilPost)." Stefan Judis | Sciencx - Accessed 2022-12-08T16:41:45+00:00. https://www.scien.cx/2018/02/08/node-js-sends-warnings-when-you-add-too-many-listeners-to-an-event-emitter-tilpost/
IEEE
" » Node.js sends warnings when you add too many listeners to an event emitter (#tilPost)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2018/02/08/node-js-sends-warnings-when-you-add-too-many-listeners-to-an-event-emitter-tilpost/. [Accessed: 2022-12-08T16:41:45+00:00]
rf:citation
» Node.js sends warnings when you add too many listeners to an event emitter (#tilPost) | Stefan Judis | Sciencx | https://www.scien.cx/2018/02/08/node-js-sends-warnings-when-you-add-too-many-listeners-to-an-event-emitter-tilpost/ | 2022-12-08T16:41:45+00:00
https://github.com/addpipe/simple-recorderjs-demo