Event Loop and EventEmitter
Event Loop
Node.js uses Events extensively, which is why it is much faster than other technologies
When a Node.js-based server starts,
it initializes variables,
declares functions,
and waits for Events to occur
In
Event-Driven applications, there is a main loop that waits for eventsAnd when an event is detected, it calls a Callback function

Event vs Callback Function
A
Callback functionis called when an asynchronous function returns a resultEvent Handlingworks based on the observer pattern
Observer pattern
A design pattern that registers a list of observers - those who observe state changes of an object - on the object, and notifies each observer in the list through methods whenever a state change occurs
It is mainly used to implement Distributed event handling systems
EventListeners functions that wait for events serve as the observers!
-> Observers wait for events, and when an event fires, the function that handles the event is executed!
events module & EventEmitter Class
: You can bind events and event handlers using Node.js's event module and EventEmitter Class
How to Use
ex)
event_handle.js
Result
EventEmitter Class
Many objects in a Node emit events
Server emits an event each time a peer connects to it
An
fs.readStreamemits an event when the file is opened
All objects which emit events are instance of
events.EventEmitter
Methods
1
addListener(event, listener)
Adds a listener at the end of the listeners array for the specified event. No checks are made to see if the listener has already been added.
Multiple calls passing the same combination of event and listener will result in the listener being added multiple times.
Returns emitter, so calls can be chained.
2
on(event, listener)
Adds a listener at the end of the listeners array for the specified event. No checks are made to see if the listener has already been added.
Multiple calls passing the same combination of event and listener will result in the listener being added multiple times. Returns emitter, so calls can be chained.
3
once(event, listener)
Adds a one time listener to the event.
This listener is invoked only the next time the event is fired, after which it is removed.
Returns emitter, so calls can be chained.
4
removeListener(event, listener)
Removes a listener from the listener array for the specified event.
Caution β It changes the array indices in the listener array behind the listener. removeListener will remove, at most, one instance of a listener from the listener array.
If any single listener has been added multiple times to the listener array for the specified event, then removeListener must be called multiple times to remove each instance.
Returns emitter, so calls can be chained.
5
removeAllListeners([event])
Removes all listeners, or those of the specified event.
It's not a good idea to remove listeners that were added elsewhere in the code, especially when it's on an emitter that you didn't create (e.g. sockets or file streams).
Returns emitter, so calls can be chained.
6
setMaxListeners(n)
By default, EventEmitters will print a warning if more than 10 listeners are added for a particular event.
This is a useful default which helps finding memory leaks.
Obviously not all Emitters should be limited to 10.
This function allows that to be increased. Set to zero for unlimited.
7
listeners(event)
Returns an array of listeners for the specified event.
8
emit(event, [arg1], [arg2], [...])
Execute each of the listeners in order with the supplied arguments.
Returns true if the event had listeners, false otherwise.
Class Methods
1
listenerCount(emitter, event)
Returns the number of listeners for a given event.
Events
1
newListener
event β String
: the event name
listener β Function
: the event handler functionThis event is emitted any time a listener is added.
When this event is triggered, the listener may not yet have been added to the array of listeners for the event.
2
removeListener
event β String The event name
listener β Function The event handler function
This event is emitted any time someone removes a listener.
When this event is triggered, the listener may not yet have been removed from the array of listeners for the event.
ex)
EventEmitter.js
Result
Last updated