0% found this document useful (0 votes)
4 views10 pages

Node.js Event Module Explained

Uploaded by

ramp68201
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views10 pages

Node.js Event Module Explained

Uploaded by

ramp68201
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Lesson:

Event Module
List of content:
Listening Event
Removing Listene
Special Even
Asynchronous Event

[Link] is a JavaScript runtime that operates asynchronously and in an event-driven manner, as per its
official documentation. Its architecture enables it to handle asynchronous tasks. The 'events' module in
[Link] allows it to emit named events that can result in corresponding functions or callbacks being invoked.
These functions or callbacks can subscribe to a specific event, and when the event is triggered, all the
callbacks that were registered in the order of their registration are executed. The EventEmitter class is
responsible for emitting events, and all objects that emit events are instances of this class. To listen or emit
an event, one can make use of the EventEmitter.

Listening events:
Before emitting any event, it must register functions(callbacks) to listen to the events.
[Link](event,
listener)[Link](event,
listener)[Link](event, listener)
The functions [Link](event, listener) and [Link](event, listener) have similar
functionality. They append the listener at the end of the listener's array for the specified event. If multiple
calls are made to the same event and listener, the listener will be added multiple times and subsequently
fired multiple times. Both functions return the emitter, so they can be chained. In contrast,
[Link](event, listener) fires only once for a specific event and is then removed from the listeners
array. It also returns the emitter, allowing for chained calls. In [Link], each event is identified by a name,
and we can trigger an event using the emit(event, [arg1], [arg2], [...]) function. We can pass any number of
arguments to the listener functions.
[Link](event, [arg1], [arg2], [...])

Simple Event:

Full Stack Web Development


Removing Listener:

The function [Link]() accepts two arguments, namely, event and listener. It removes the
specified listener from the listeners array that is subscribed to the given event. On the other hand,
[Link]() removes all listeners that are subscribed to the specified event from the
listeners array.

Note:

Removing the listener from the array will change the sequence of the listener’s array, hence it must be carefully
used. The [Link]() will remove at most one instance of the listener which is in front of the
queue.

Full Stack Web Development


If we register fun1 twice and fun2 once and then call [Link]('myEvent', fun1), one instance
of fun1 will be removed, leaving one instance of fun1 and one instance of fun2 still subscribed to 'myEvent'.
Finally, removing all listeners using removeAllListeners() will remove all listeners that were subscribed to
'myEvent'.

In addition to the aforementioned methods, there are other methods related to the maximum number of
listeners that can be registered for a single event. By default, a maximum of 10 listeners can be registered for
any single event. To modify this default value for all instances of EventEmitter, we can use the
[Link] property. We can use the [Link]() function to retrieve
the maximum number of listeners allowed, which can be set using setMaxListeners() or is set to the default
value of 10 if not explicitly set. It is important to note that although the maximum number of listeners is not a
hard limit, if a new instance is added, EventEmitter will allow it but will issue a warning message indicating a
possible memory leak.

Full Stack Web Development


[Link](): It returns an array of listeners for the specified event.

Full Stack Web Development


[Link](): It returns the number of listeners listening to the specified event.

[Link](): It will add the one-time listener to the beginning of the array.

[Link](): It will add the listener to the beginning of the array.

Full Stack Web Development


Special Events: 

Whenever new listeners are added, EventEmitter instances emit the 'newListener' event, and whenever existing
listeners are removed, they emit the 'removeListener' event.

Event : ‘newListener’ The EventEmitter instance will emit its own ‘newListener’ event before a listener is added to
its internal array of listeners. Listeners registered for the ‘newListener’ event will be passed to the event name
and reference to the listener being added. The event ‘newListener’ is triggered before adding the listener to the
array.

Event: ‘removeListener’ The ‘removeListener’ event is emitted after a listener is removed.

Event: ‘error’ When an error occurs within an EventEmitter instance, the typical action is for an ‘error’ event to be
emitted. If an EventEmitter does not have at least one listener registered for the ‘error’ event, and an ‘error’ event
is emitted, the error is thrown, a stack trace is printed, and the [Link] process exits.

Full Stack Web Development


Asynchronous events:

The listeners that were registered are called by the EventEmitter synchronously, in the order of their registration.
Nevertheless, it is possible to perform asynchronous calls using either setImmediate() or [Link]().

Full Stack Web Development


The EventEmitter class allows developers to create and manage custom events that can be triggered at
specific times in the application.

Next,an instance of the EventEmitter class is created using the new keyword.

Then, the code defines an asynchronous listener function using the on() method of the eventEmitter instance.
This listener function is triggered when the event named myEvent is emitted.

The listener function takes a message msg as a parameter and prints it to the console using the [Link]()
method. Note that the message is printed using setImmediate() method, which allows the function to execute
asynchronously.

Next, the code declares another listener function fun that takes a message msg as a parameter and prints it to
the console using the [Link]() method.

Full Stack Web Development


The fun function is then registered as a listener for the myEvent event using the on() method of the eventEmitter
instance.

Finally, the code emits the myEvent event using the emit() method of the eventEmitter instance and passes the
message "Event occurred" as a parameter.

When the myEvent event is emitted, both listener functions are called asynchronously. The fun function prints
the message "Message from fun: Event occurred" to the console, while the asynchronous listener function prints
the message "Message from async: Event occurred" to the console.

Lets look at the application of [Link] in this case.

Here, we use [Link]() instead of setImmediate() to schedule the asynchronous operation. The rest of
the code remains the same.

Note that in this case, both listener functions are still called in the order of their registration, but the
asyncOperation() function is executed asynchronously in the next iteration of the event loop using
[Link]().

Full Stack Web Development

You might also like