Event Emitter in Node.js

Many objects in Node.js generate events, for example net.Server generates an event every time a peer connection to it, or fs.readStream generates an event when a file is opened. All of these objects are instances of the events.EventEmitter class in Node.js.

Many objects in Node.js generate events, for example net.Server generates an event every time a peer connection to it, or fs.readStream generates an event when a file is opened. All of these objects are instances of the events.EventEmitter class in Node.js.

The EventEmitter class in Node.js

EventEmitter class is located in events Module. This class is accessed via the following syntax:

 // Khai bao events Module var events = require ( 'events' ); // Tao mot doi tuong eventEmitter var eventEmitter = new events . EventEmitter (); 

When an EventEmitter experiences any errors, it will generate an Error Event. When a new Listener is added, the 'newListener' event will be triggered and a Listener will be removed, the 'removeListener' event will be triggered.

Event Emitter provides many attributes like on or emit . The on attribute is used to bind a function to the event, and emit is used to trigger an event.

Methods of the EventEmitter class in Node.js

Stt Method & Description1 addListener (event, listener)
Add a Listener to the end of the Listener array for a specific 2 on event (event, listener)
Add a Listener to the end of the Listener array for a specific event 3 times (event, listener)
Add a One-Time Listener to the event. This listener will only be called when the event is activated, then it will be deleted 4 removeListener (event, listener)
Removing a Listener from the Listener array for an event. 5 removeAllListeners ([event])
Delete all Listener of an event 6 setMaxListeners (n)
By default, the EventEmitters class will print a warning if you add more than 10 Listener for a specific event. This is quite useful, because it will help find errors that cause memory leaks. Of course, not all Emitters need to be limited to a number of 10. This function allows you to increase that number. Set it to 0 to not limit the amount of Listener needed 7 listeners (event)
Returns an array containing Listener for a specific event 8 emit (event, [arg1], [arg2], [.])
Execute each Listener with the given parameters. Returns true if the event has Listener, and false if not

Events of the EventEmitter class in Node.js

Stats Events & Description1 newListener
  1. event - String form, representing the event name

  2. listener - Name of event handler

This event is generated whenever you add a Listener. When this event is triggered, the Listener may not have been added to the Listener array of the event

2 removeListener
  1. event - String form, representing the event name

  2. listener - Name of event handler

This event occurs whenever someone deletes a Listener. When an event is triggered, this Listener has not been removed from the Listener array of events

The example illustrates the EventEmitter class in Node.js

Create a js file with the name main.js with the Node.js content as shown below. In main.js, you first declare events Module using the require () method. Next, you use the addListener () method to add a Listener to an event, and use the on and emit properties to implement the features described above.

 var events = require ( 'events' ); var eventEmitter = new events . EventEmitter (); // listener # 1 var listner1 = function listner1 () { console . log ( 'listener1 has been tested.' ); } // listener # 2 var listner2 = function listner2 () { console . log ( 'listener2 must be tested.' ); } // Good connection with listner1 eventEmitter . addListener ( 'connection' , listner1 ); // Gan connection with the listner2 eventEmitter . on ( 'connection' , listner2 ); var eventListeners = require ( 'events' ). EventEmitter . listenerCount ( eventEmitter , 'connection' ); console . log ( eventListeners + "Event Listner is listening to the connection" ); // Fire the connection event eventEmitter . emit ( 'connection' ); // Remove the binding of function listner1 eventEmitter . removeListener ( 'connection' , listner1 ); console . log ( "Bay gio, Listner1 will not listen." ); // Fire the connection event eventEmitter . emit ( 'connection' ); eventListeners = require ( 'events' ). EventEmitter . listenerCount ( eventEmitter , 'connection' ); console . log ( eventListeners + "Event Listner is listening to the connection" ); console . log ( "Ket process." ); 

Run main.js to see the result:

 $ node main . js 

Check the result:

 2 Event Listner is listening to the connection 
listener1 has been tested.
listener2 has been tested.
Fly, Listner1 will not listen.
listener2 has been tested.
An Event Listner is listening to the connection
Let's get married.

According to Tutorialspoint

Previous article: Event Loop in Node.js

Next lesson: Concept of Buffer in Node.js

5 ★ | 1 Vote