Learn Event and EventEmitter in nodejs

Tram Ho

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.

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.

Note: The on and addListener methods are completely similar

Methods of EventEmitter class

NoMethodDescribe
firstaddListener (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.
Texton (event, listener)This method is exactly 100% identical to the addListener method
Textonce (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.
TextremoveListener (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.
TextremoveAllListeners ([event])Remove all listeners, or remove all listeners of a specified event.
TextsetMaxListeners (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.
Textlisteners (event)Returns an array of listeners for the specified event.
Textemit (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

The emit method will trigger the clicked event and it will call the clicked event being listened to on

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

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).

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

Share the news now

Source : Viblo