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
event - String form, representing the event name
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 removeListenerevent - String form, representing the event name
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
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