Event and EventEmitter
Node.js is based on an asynchronous event-driven architecture in which certain objects called emitters periodically emit (emit) events that cause Listener Objects to be called.
When the EventEmitter object emits an event, all functions attached to that particular event are called synchronously.
Every action on the computer is considered an event, such as writing a file, reading a file, connecting to a database, reading a database, retrieving data from a database, etc. .
To use events in nodejs, we first need to use an existing module in nodejs, events. All event methods or properties are an instance of EventEmitter, so in order to use these methods or properties, we need to create an EventEmitter object.
1 2 3 | var events = require('events'); var eventEmitter = new events.EventEmitter(); |
In NodeJS, Listener is a function, it will be called to execute when the event occurs. There may be 0, 1 or more listeners being attached to that event.
1 2 3 4 5 6 7 8 9 | eventEmitter.on('clicked', function() { console.log('Something is clicked!'); }) eventEmitter.addListener('clicked', function() { console.log('Something is clicked!'); }) |
Note: The on and addListener methods are completely similar
Methods of EventEmitter class
No | Method | Describe |
---|---|---|
first | addListener (event, listener) | Add a listener to the end of the listeners array for the specified event. This method does not check if this listener has been added or not. |
Text | on (event, listener) | This method is exactly 100% identical to the addListener method |
Text | once (event, listener) | Add a listener to the listeners array of the specified event. But this listener is only called once when the event occurs. It is then removed from the array. |
Text | removeListener (event, listener) | Removes a listener from the listeners array of the specified event. If a listener has been added to this array multiple times, to remove this listener you need to call this method multiple times. |
Text | removeAllListeners ([event]) | Remove all listeners, or remove all listeners of a specified event. |
Text | setMaxListeners (n) | By default, EventEmitter will print a warning if more than 10 listeners are added for a specific event. This is a useful default to help find memory leaks. You can set another number, or set it to 0 if you want it to be unlimited. |
Text | listeners (event) | Returns an array of listeners for the specified event. |
Text | emit (event, [arg1], [arg2], […]) | Execute each listener individually in the array, with parameters. Returns true if the array has at least one listener, otherwise returns false. |
Examples of Event and EventEmitter
1 2 3 4 5 6 | eventEmitter.on('clicked', function() { console.log('Something is clicked!'); }) eventEmitter.emit('clicked'); |
The emit
method will trigger the clicked
event and it will call the clicked
event being listened to on
1 2 3 4 5 6 | eventEmitter.on('clicked', function(button) { console.log(button + ' is clicked!'); }) eventEmitter.emit('clicked', 'button 1'); |
We can also pass the argument to the callback when the event is triggered, as the above example shows the button 1 is clicked
.
We can also pass multiple arguments to the event corresponding to the amount we pass into the emit
method
1 2 3 4 5 6 | eventEmitter.on('clicked', function(button, a, b, c) { console.log(button + ' is clicked!'); }) eventEmitter.emit('clicked', 'button 1', '1', '2', '3'); |
The variables a, b, c will respectively be ‘1’, ‘2’, ‘3’.
Introduction to inherits method
One way that other Objects can also use event methods is to use inherits
in the util
module (which is also an existing nodejs module).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | var events = require('events'); var util = require('util'); var Students = function(name) { this.name = name; } util.inherits(Students, events.EventEmitter); var max = new Students('max'); max.on('scored', function(marks) { console.log(max.name + ' scores '+ marks+ ' marks'); }) max.emit('scored', 95); var tom = new Students('tom'); tom.on('scored', function(marks) { console.log(tom.name + ' scores '+ marks+ ' marks'); }) tom.emit('scored', 60); |
And results:
By using inherits, Class Students can inherit methods of Class EventEmitter (on, emit, …), so objects in this class can also call and use the above methods.
Reference source:
https://www.udemy.com/course/learn-node-js-complete-from-very-basics-to-advance
https://o7planning.org/en/11953/huong-dan-nodejs-eventemitter
https://www.tutorialspoint.com/nodejs/nodejs_event_emitter.htm