AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

Node.js Event emitter


<< Previous Page
Nodejs tutorial
Next Page >>
Nodejs tutorial

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:

Syntax

const EventEmitter = require('events');

 

Create an event emitter

Create an event emitter instance and register a couple of callbacks

const my_Emitter = new EventEmitter();

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.

my_Emitter.emit('eventDue');

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

my_Emitter.on('status', (code, msg)=> console.log(`Got ${code} and ${msg}`));

Emitting the event with parameters:

my_Emitter.emit('status', 400, 'ok');

Output:
in the console will have as below:

Got 400 and ok

Unregistering events

my_Emitter.off('eventDue', a1);

Now if you emit the event as follows, nothing will happen and it will be a noop:

my_Emitter.emit('eventDue); // 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.

Example:
var events = require('events');
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:

E:\Users\My_Name>node demo_text_eventemitter.js
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

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Upload Image, display, edit and delete i...
Login Form with Image background...
How to Create an Image with Transparent ...
Portfolio Gallery Website with filtering...
Simple pagination script PHP MySQLi...
Center Image in div...
Image Hover Overlay Fade...
Sticky image / element / div on scroll...
Responsive images...
Create rounded image in HTML CSS...
Add border around image...
Position Text Over an Image HTML CSS res...
Create a Slideshow Images Gallery...
Create a Sticky Sidebar...
Search bar using CSS HTML...
Shrink Navigation Menu on Scroll...
How to Create Accordion HTML Templates...
Dropdown menu in the navigation bar...
Responsive Top Navigation Bar with mobil...
Split horizontal navigation bar...