Events and EventEmitter

var events = require(‘events’);

var eventEmitter = new events.EventEmitter();

var ringBell = function ringBell()
console.log(‘ring ring ring’);
eventEmitter.on(‘doorOpen’, ringBell);



The above program is not written by me. Now am learning node js. I’am stuck with the topic of Events and EventEmitter. I’ve look up for a lot of videos and Tutorials, i coudn’t find anything that understand about this topic.
If you can help me with this topic please tell about the Events and also tell what you mean (eventEmitter.on(‘doorOpen’, ringBell);) “ON” in this program.

I do not use events in NodeJS very much, but I believe I may be able to provide some insight here.

So by importing the events package with require('events'), you gain access to the EventEmitter() class that comes defined with a whole set of functions.

Two of these functions appears to be .on and .emit. They both work together to provide the event-calling nature of the imported EventEmitter() class available from require('events').

var myEmitter = new events.EventEmitter(); // events imported above
myEmitter.on(<name-of-event>, <name-of-function>);

.on is listening for .emit to execute with <name-of-event>. Once it does, the second argument of .on <name-of-function> executes. In a sense, .on is the event-listener while .emit is the event-caller. .on responds to .emit if .emit is emitting an event that matches up with what .on is listening for.

Of course, they both communicate through the myEmitter instance created from the EventEmitter() class. Emitting from one instance of myEmitter will not be listenable from anotherEmitter instance.

Hopefully this helps!

1 Like

I’ve one more doubt to clear about your answer

“Emitting from one instance of myEmitter will not be listenable from anotherEmitter instance.”

can’t we create one more emitter like class

var events = require(‘events’);

var myEmitter = new events.EventEmitter() // one

var secondEmitter = new events.EventEmitter() //Two

can we call the secondEmitter also like the firstone using event-listener and the event-caller.

I don’t know what the stupidity am asking, but I have to clear that doubts too.

You 100% can. The secondEmitter is its own instance of EventEmitter() which means

secondEmitter.on('myEvent', executeFunction);

… will not work, but

secondEmitter.on('myEvent', executeFunction);

… will work given that secondEmitter is listening and emitting on the same instance of the EventEmitter() class.

// Import events module
var events = require(‘events’);

var eventEmitter = new events.EventEmitter(); //first instance

var secondEmitter = new events.EventEmitter(); //second instance

var ringBell = function ringBell() //create a function ‘ringBell’
console.log(“ring ring ring”); //Expand

var doorOpen = function doorOpen() //create a function ‘doorOpen’
console.log(“welcome to the world of Programming!”); //expand


secondEmitter.on(“openthedoor”, doorOpen);


eventEmitter.on(“bell_ring”, ringBell);


Everything checks out! Although, I see you declared a secondEmitter that seems to be unused. Other than that, I just pasted your code into a node terminal and it gave the expected output.

first of all, Thank you very much for your replay and your patience.
Now I understand about the basic of the Events.
Everybody speaks a lot more about Events, but no one tell what is the basic of these things.

I’ve just run the project , and its successfully executed.
Again thank you for the time.