The Node events module
The events module of Node.js provides the EventEmitter class
The events module provides us the EventEmitter class, which is key to working with events in Node.
I published a full article on that, so here I will just describe the API without further examples on how to use it.
const EventEmitter = require('events')
const door = new EventEmitter()
The event listener eats its own dog food and uses these events:
newListenerwhen a listener is addedremoveListenerwhen a listener is removed
Here’s a detailed description of the most useful methods:
- emitter.addListener()
- emitter.emit()
- emitter.eventNames()
- emitter.getMaxListeners()
- emitter.listenerCount()
- emitter.listeners()
- emitter.off()
- emitter.on()
- emitter.once()
- emitter.prependListener()
- emitter.prependOnceListener()
- emitter.removeAllListeners()
- emitter.removeListener()
- emitter.setMaxListeners()
emitter.addListener()
Alias for emitter.on().
emitter.emit()
Emits an event. It synchronously calls every event listener in the order they were registered.
emitter.eventNames()
Return an array of strings that represent the events registered on the current EventListener:
door.eventNames()
emitter.getMaxListeners()
Get the maximum amount of listeners one can add to an EventListener object, which defaults to 10 but can be increased or lowered by using setMaxListeners()
door.getMaxListeners()
emitter.listenerCount()
Get the count of listeners of the event passed as parameter:
door.listenerCount('open')
emitter.listeners()
Gets an array of listeners of the event passed as parameter:
door.listeners('open')
emitter.off()
Alias for emitter.removeListener() added in Node 10
emitter.on()
Adds a callback function that’s called when an event is emitted.
Usage:
door.on('open', () => {
console.log('Door was opened')
})
emitter.once()
Adds a callback function that’s called when an event is emitted for the first time after registering this. This callback is only going to be called once, never again.
const EventEmitter = require('events')
const ee = new EventEmitter()
ee.once('my-event', () => {
//call callback function once
})
emitter.prependListener()
When you add a listener using on or addListener, it’s added last in the queue of listeners, and called last. Using prependListener it’s added, and called, before other listeners.
emitter.prependOnceListener()
When you add a listener using once, it’s added last in the queue of listeners, and called last. Using prependOnceListener it’s added, and called, before other listeners.
emitter.removeAllListeners()
Removes all listeners of an event emitter object listening to a specific event:
door.removeAllListeners('open')
emitter.removeListener()
Remove a specific listener. You can do this by saving the callback function to a variable, when added, so you can reference it later:
const doSomething = () => {}
door.on('open', doSomething)
door.removeListener('open', doSomething)
emitter.setMaxListeners()
Sets the maximum amount of listeners one can add to an EventListener object, which defaults to 10 but can be increased or lowered.
door.setMaxListeners(50) download all my books for free
- javascript handbook
- typescript handbook
- css handbook
- node.js handbook
- astro handbook
- html handbook
- next.js pages router handbook
- alpine.js handbook
- htmx handbook
- react handbook
- sql handbook
- git cheat sheet
- laravel handbook
- express handbook
- swift handbook
- go handbook
- php handbook
- python handbook
- cli handbook
- c handbook
subscribe to my newsletter to get them
Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing flavio@flaviocopes.com. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.