Node.js Event emitter
Node.js Tutorial » Node.js Event emitter
What is NodeJS Event emitter?
First: Every action on a computer is an event as: when a connection is made or a file is opened
EventEmitter is a module that facilitates communication / interaction between objects in Nodejs.
EventEmitter is at the heart of Node js event-based asynchronous architecture.
Many of Node js' built-in modules inherit from EventEmitter, including prominent frameworks such as Express.js.
The concept is simple: the emitter objects emit named events that cause listeners of previously recorded listeners to call. So an emitting object has practically two main characteristics:
- Emitting name events.
- Registering and unregistering listener functions.
Syntax
Create an event emitter
Create an event emitter instance and register a couple of callbacks
function a1() {
console.log('an event occurred now!');
}
function a2() {
console.log('yet another event occurred now!');
}
my_Emitter.on('eventDue', a1); // Register for eventOne
my_Emitter.on('eventDue', a2); // Register for eventOne
When the event ‘eventDue’ is emitted, both the above callbacks should be invoked.
Output: in the console will be as:
an event occurred now!
yet another event occurred now!
Related subjects:
node.js http module
node.js modules
global objects nodejs
Tags: Node.js Event emitter, event emitter on, once, async, use cases, between modules, singleton, eventemitter off, source code, eventemitter memory leak
What is NodeJS event emitter?
What is event and event emitter?
How do I use EventEmitter in node JS?
What is the use of event emitter?
Event with callback parameters
Registering for the event with callback parameters
Emitting the event with parameters:
Output:
in the console will have as below:
Unregistering events
Now if you emit the event as follows, nothing will happen and it will be a noop:
The EventEmitter Object
You can assign event handlers to your own events with the EventEmitter object.
we have created a function that will be executed when a "scream" event is fired.
To fire an event, use the emit() method.
var event_Emitter = new events.Event_Emitter();
//Create an event handler:
var my_Event_Handler = function () {
console.log('Hi, this is my sacript');
}
//Assign the event handler to an event:
event_Emitter.on('script', my_Event_Handler);
//Fire the 'script' event:
event_Emitter.emit('script');
Output:
Hi, this is my sacript
Node.js Event emitter, event emitter on, once, async, use cases, between modules, singleton, eventemitter off, source code, eventemitter memory leak
What is NodeJS event emitter?
What is event and event emitter?
How do I use EventEmitter in node JS?
What is the use of event emitter?
Node.js Event emitter - nodejs
This tool makes it easy to create, adjust, and experiment with custom colors for the web.

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.

Find here examples of creative and unique website layouts.

Find here examples of creative and unique website CSS HTML menu.