Every action on a computer is like an event. For, e.g., when a connection is made, or you open any file. Objects inside Node.js can fire events, like the readStream object fires events when opening or closing a file.
Node.js is an asynchronous event-driven JavaScript runtime. Its event-driven architecture allows it to perform asynchronous tasks. It has a built-in module called the 'events' module, where you can create, fire, and listen for your events. When a function (Callback) listens to a particular event to occur, all the callbacks subscribing to that event are fired one by one in the sequence in which they were registered.
All the objects that emit events are instances of the EventEmitter class.
The EventEmitter class
The events module provides an EventEmitter class, which is the key to working with events in Node.js. The event can be emitted or listened to only with the help of EventEmitter.
Syntax:
JS
const EventEmitter = require('events'); var eventEmitter = new EventEmitter.EventEmitter();
The EventEmitter is a class. By convention, it is in camel casing. All event properties and methods are the instances of an EventEmitter object. You can access them by creating an object of the EventEmitter class called eventEmitter.
The EventEmitter Object
An EventEmitter object has two functions:
Emit a named event.
Attach or detach listeners to the named event.
You can assign event handlers to the events with the EventEmitter object.
For example, we have created a function executed when a "scream" event is fired. To fire an event, use the emit() method.
var event = require('events'); var eventEmitter = new event.EventEmitter();
//Create an event handler var handler = function () { console.log('I hear a scream!'); }
//Assign the event handler to an event eventEmitter.on('scream', handler);
//Fire the event: eventEmitter.emit('scream');
Output:
Listening events:
The event listener has these in-built events:
newListener: Add a new listener.
removeListener: Remove a listener.
Methods in the EventEmitter class
eventEmitter.addListerner()
Syntax
eventEmitter.addListener(event, listerner)
It is an alias for the eventEmitter.on().
eventEmitter.emit()
Syntax
eventEmitter.emit("eventName") // pass the event’s name
This method emits an event. It will synchronously call every event listener in the order they were registered.
eventEmitter.eventNames()
Syntax
eventEmitter.eventNames()
Returns an array of strings that represent the events registered on the current eventEmitter object.
eventEmitter.getMaxListeners()
Syntax
eventEmitter.getMaxListeners()
Give the maximum amount of listeners that can be added to an EventEmitter object which by default is 10 but can be increased or decreased by using setMaxListeners().c.opy
eventEmitter.listenerCount()
Syntax
eventEmitter.listenerCount('open')
Returns the number of listeners of the event passed as a parameter.
eventEmitter.listeners()
Syntax
eventEmitter.listeners('open')
Returns an array of listeners of the event that is passed as a parameter.
eventEmitter.off()
It is an Alias for eventEmitter.removeListener() added in Node.js 10.
It adds a callback function that is called when an event is emitted.
eventEmitter.once()
Syntax
eventEmitter.once('my-event', () => { //call callback function once })
It adds a callback function when an event is emitted for the first time after registering. This callback is never going to be called again.
eventEmitter.prependListener()
When you use on or addListener to add a listener, it's called last since it's added last in the listener's queue. But using prependListener, it's added and called before other listeners.
eventEmitter.prependOnceListener()
When you use once to add a listener, it's called last since it's added last in the listener's queue. But using prependOnceListener, it's added and called before other listeners.
eventEmitter.removeAllListeners()
Syntax
eventEmitter.removeAllListeners('open')
It removes all listeners of the EventEmitter object listening to a specific event.
It removes a specific listener by saving the callback function to a variable when added so that you can reference it later.
eventEmitter.setMaxListeners()
Syntax
eventEmitter.setMaxListeners(50)
It sets the maximum number of listeners added to an EventEmitter object but can be increased or decreased by default.
Asynchronous events:
The EventEmitter object calls the listeners synchronously in the order in which they were registered. However, we can perform asynchronous calls on the listeners using setImmediate() or process.nextTick().
Frequently Asked Questions
Q1: What is the event module?
Ans: It is a Node.js module that provides you with an EventEmitter class that allows you to manage the events in the node application.
Q2: What is node js event emitter?
Ans: The EventEmitter is a class of ‘events’ mode in Node.js. It contains functions and methods that facilitate communication/interaction between objects.
Q3: What is the difference between events and callbacks in node JS?
Ans: In NodeJS, events are the same as for callbacks. A callback function is called to fire events based on the observer when a function's execution is complete. Every event has listeners, and when an event is triggered, the listener function associated with it begins the execution process.
Q4: Is the event listener a callback?
Ans: The event listener can be a callback function or an object that implements EventListener and has a handleEvent() method that acts as a callback function.
Key takeaways
In this blog, we learned about the ‘events’ module in node.js. We saw the methods and their syntax provided by EventEmitter class of the module.
Coding Ninjas provides a whole course on NodeJS. It provides basic to Advance level understanding. Enroll today and build amazing websites using Node. Click on the given link to enroll.