Node.js uses events module to create and handle custom events. The EventEmitter class can be used to create and handle custom events module.
The syntax to Import the events module are given below:
const EventEmitter = require('events');
All EventEmitters emit the event newListener when new listeners are added and removeListener when existing listeners are removed. It also provide one more option:
Default Value: false It automatically captures rejections.
Listening events: Before emits any event, it must register functions(callbacks) to listen to the events.
eventEmitter.addListener(event, listener) eventEmitter.on(event, listener)
eventEmitter.on(event, listener) and eventEmitter.addListener(event, listener) are pretty much similar. It adds the listener at the end of the listener’s array for the specified event. Multiple calls to the same event and listener will add the listener multiple times and correspondingly fire multiple times. Both functions return emitter, so calls can be chained.
Emitting events: Every event is named event in nodejs. We can trigger an event by emit(event, [arg1], [arg2], […]) function. We can pass an arbitrary set of arguments to the listener functions.
eventEmitter.emit(event, [arg1], [arg2], [...])
Removing Listener: The eventEmitter.removeListener() takes two argument event and listener, and removes that listener from the listeners array that is subscribed to that event. While eventEmitter.removeAllListeners() removes all the listener from the array which are subscribed to the mentioned event.
eventEmitter.removeListener(event, listener) eventEmitter.removeAllListeners([event])
Message from geek1: Event occurred Message from geek2: Event occurred
We registered two times geek1 and one time geek2. For calling eventEmitter.removeListener(‘myEvent’, geek1) one instance of geek1 will be removed. Finally, removing all listener by using removeAllListeners() method that will remove all listeners to myEvent.
Special Events: All EventEmitter instances emit the event ‘newListener’ when new listeners are added and ‘removeListener’ existing listeners are removed.
- Event: ‘newListener’ The EventEmitter instance will emit its own ‘newListener’ event before a listener is added to its internal array of listeners. Listeners registered for the ‘newListener’ event will be passed to the event name and reference to the listener being added. The event ‘newListener’ is triggered before adding the listener to the array.
eventEmitter.once( 'newListener', listener) eventEmitter.on( 'newListener', listener)
- Event: ‘removeListener’ The ‘removeListener’ event is emitted after a listener is removed.
eventEmitter.once( ‘removeListener’, listener) eventEmitter.on( 'removeListener’, listener)
- Event: ‘error’ When an error occurs within an EventEmitter instance, the typical action is for an ‘error’ event to be emitted. If an EventEmitter does not have at least one listener registered for the ‘error’ event, and an ‘error’ event is emitted, the error is thrown, a stack trace is printed, and the Node.js process exits.
The listener is added to removeListener The listener is added to myEvent The listener is added to myEvent The listener is removed from myEvent Message from geek2: Event occurred Attention! There was an error
Please Login to comment...