![]() |
VOOZH | about |
An event represents an action or occurrence, such as a user interaction, file operation, or incoming request—that the system can detect and respond to. Events are handled using the EventEmitter class from the built-in events module, enabling Node.js to execute code asynchronously whenever specific actions occur.
EventEmitter is an object that can generate events in NodeJS.At the core of the NodeJS event system is EventEmitter class, allowing objects to emit named events that can be listened to by other parts of your application. It is included in the built-in events module.
Step 1: To use events in your application, first import the events module and create an instance of the EventEmitter class.
const EventEmitter = require('events');
const myEmitter = new EventEmitter();
Step 2: You can register listeners for specific events using the on() method. The first argument is the event name, and the second is the callback function that gets executed when the event is emitted.
myEmitter.on('event', () => {
console.log('An event occurred!');
});
Step 3: To trigger an event, use the emit() method with the event name as the first argument. It will call all the listeners attached to that event.
myEmitter.emit('event');
Output:
An event occurred!Before emitting an event, you must first register listeners (callback functions) that will listen for the event.
Syntax:
eventEmitter.addListener(event, listener)
eventEmitter.on(event, listener)
eventEmitter.once(event, listener)
It will add the one-time listener to the beginning of the array.
Syntax:
eventEmitter.prependOnceListener(event, listener)It will add the listener to the beginning of the array.
Syntax:
eventEmitter.prependListener(event, listener)To remove a listener for an event, use removeListener() method with the event name and listener function. If you want to remove all listeners for an event, use removeAllListeners().
Syntax:
eventEmitter.removeListener(event, listener)
eventEmitter.removeAllListeners([event])
Note: Removing a listener with
eventEmitter.removeListener()must be done carefully, as it removes only one instance of the specified listener (the first occurrence in the queue) and also alters the order of the remaining listeners in the array.
Example:
Output:
Message from fun4 (once): First emit
Message from fun3: First emit
Message from fun1: First emit
Message from fun2: First emit
Message from fun3: Second emit
Message from fun2: Second emit
It returns an array of listeners for the specified event.
Syntax:
eventEmitter.listeners(event)It returns the number of listeners listening to the specified event.
Syntax:
eventEmitter.listenerCount(event)Example:
Output:
[ [Function: fun2], [Function: fun1] ]
2
Message from fun2: Event occurred
Message from fun1: Event occurred
All EventEmitter instances emit the event 'newListener' when new listeners are added and 'removeListener' existing listeners are removed.
eventEmitter.once( 'newListener', listener)
eventEmitter.on( 'newListener', listener)
eventEmitter.once( ‘removeListener’, listener)
eventEmitter.on( 'removeListener’, listener)
eventEmitter.on('error', listener)Output:
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 fun2: Event occurred
whoops! there was an error
The EventEmitter calls all listeners synchronously in order to which they were registered. However, we can perform asynchronous calls by using setImmediate() or process.nextTick().
Output:
Message from fun: Event occurred
Message from async: Event occurred