Introduction
This article explains the EventEmitter in NodeJS. To access the EventEmitter class, use:
require('events').EventEmitter
API methods are accessed through an instance of the EventEmitter class. For this you need to create an object of the EventEmitter class, then you can access the method.
For example
var obj=new EventEmitter();
Using that you can access the API method via an EventEmitter instance. Let's see how in the following:
obj.emitEvent();
In Node, we do not have the DOM so we use the EventEmitter class. This class has mainly two methods, "on" and "emit". I have given the example and sample use of these methods in the NodeJS environment.
EventEmitter Class
The event module contains the EventEmitter class. The Util package provides a way to inherit from one class to another class. The "EventEmitter" class allows us to listen for events and assign actions to run when those events occur. The EventEmitter class is based on a publish/subscribe model because we can subscribe to events and then publish them.
When we use the EventEmitter class we need to require an events module in the program, for example:
var ev=require("events");
The ev object will then have a single property, which is the EventEmitter class itself.
Objects that emit events are instances of "events.EventEmitter." You can access them using:
require("events")
Functions can be attached to objects, to be executed when an event is emitted; these functions are listeners. Inside a listener function, "this" refers to the "EventEmitter" that the listener was attached to.
Let's see a simple example of the EventEmitter class. Create a file with a .js extension and execute the code in a Node.js command prompt.
Step 1
Open the editor and write the following code and save the file with a .js extension as in the following figure:
Step 2
Now open the node.js command prompt. If you don't have the node.js environment then you can download it from this link
Get NodeJS Framework.
Step 3
Now write the following command in a command prompt as in the following figure:
In the code snippets above we have created an object of the EventEmitter class, this object has two main methods for events, "on" and "emit". The on method has two parameters, the first one is the event name, "GetEvent" and the second parameter is the function that will be called when the event occurs.
Features of EventEmitter class
- This class is for managing events.
- It can be extended to provide event functionality in other classes.
- It emits events.
- It listens to and handles those events.
- Something that drives the two, causing events to be emitted.
EventEmitter Methods
- emitobj.on(event, listener)
- emitobj.emit(event,[arg1],[arg2],..[argN])
- emitobj.once(event, listener)
- emitobj.removeListener(event, listener)
emitobj.on(event, listener)
Adds a listener to the end of the listeners for the specified event. Let's see the example.
emitobj.emit(event,[arg1],[arg2],...[argN])
Execute each of the listeners in order with the supplied arguments, returns true if event has listeners, false otherwise. For example, the figures given below.
Create a file in an editor and save the file with FirstModule.js extension.
Now create another file and save it with SecondModule.js.
Now open a node.js command prompt and write the following command as in the following figure:
emitobj.once(event, listener)
Adds a one-time listener for the event. The listener is invoked only the first time when the event is fired then it is removed.
emitobj.removeListener(event, listener)
Event "string" the event name and the listener "Function" is the event handler function. This event is emitted anytime someone removes a listener. It is unspecified if the listener is in the list returned by "emitobj.listerner(event)."
Summary
In this article, you saw how the EvenEmitter class works. The EventEmitter class will help you to write impressive event-based Node modules.