In previous Node.js learning articles, Mytour introduced readers to the concept of Event in Node.js. In this article, Mytour will further elaborate on EventEmitter in Node.js.
Exploring the concept of EventEmitter in Node.js
What is EventEmitter in Node.js?
Many objects in Node.js will emit events, such as net.Server emitting events whenever a peer connects to it, or fs.readStream emitting events when a file is opened. All these event-emitting objects are variants of the EventEmitter class.
As mentioned by Mytour above, the EventEmitter class resides in the event module. To access the EventEmitter class, you use the code snippet below:
// Import events module
Import the events module:
Create an eventEmitter object:
Instantiate an eventEmitter object:
When an EventEmitter encounters any error, it emits an 'error' event. When a new Listener is added, the newListener event is triggered, and when a Listener is removed, the removeListener event is triggered.
EventEmitter provides various attributes such as on and emit. The on attribute is used to bind with an event function, and the emit attribute is used to trigger the event.
Example of handling events in Node.js
Here is an example of handling events in Node.js:
In the example above, the first step is to import the events module, then create an object of the EventEmitter class. The next step is to define the event handler function using the on() function. The on() method requires the event name to handle and a callback function that is called when an event is raised.
The emit() function will trigger the specified event. The first parameter is the name of the event as a string, followed by arguments.
An event can be triggered even when the argument is 0 or other values. You can specify any name for a custom event in the emit() function.
Additionally, you can also use the addListener() method to register an event as shown below:
Example of EventEmitter in Node.js
Methods of EventEmitter in Node.js
Here is a table listing important methods of the EventEmitter class in Node.js:
Common EventEmitter Patterns
Two common patterns are used to enhance and link events using the EventEmitter class in Node.js:
1. Returning EventEmitter from a function.
2. Extending the EventEmitter class.
Returning EventEmitter from a function
In this pattern, the constructor function returns an EventEmitter object, used to emit events within a function. This EventEmitter object can be used to subscribe to events.
For a clearer understanding, refer to the example below:
Output:
In the LoopProcessor() function, first, we create an object of the EventEmitter class, then use it to emit the BeforeProcess and AfterProcess events. Finally, return an EventEmitter object from the function.
Now we can use the return value of the LoopProcessor function to bind these events using the on() or addListener() function.
Extending the EventEmitter class
In this pattern, we can extend the constructor function from the EventEmitter class to emit events.
Here's an example of extending the EventEmitter class:
Output:
In the example above, the LoopProcessor constructor function extends the EventEmitter class using the util.inherits() method from the utility module. Therefore, we can utilize EventEmitter methods with the LoopProcessor object to handle its own events.
Through this approach, you can leverage the EventEmitter class to emit and handle custom events in Node.js.
This article by Mytour just introduced you to what EventEmitter is in Node.js. Additionally, readers can refer to other articles on Mytour to learn how to install and create Node.js applications.
