Node.js is built around the asynchronous event driven mechanism, so it works very fast and flawlessly. Much of the Node.js core API is built around an idiomatic asynchronous event-driven architecture in which certain kinds of objects (called "emitters") periodically emit named events, which enable the Function objects ("listeners") to be called. All the event-based Node.js libraries are EventEmitters.
All objects, which emit the events are the instances of the EventEmitter class. These objects expose an eventEmitter.on() function, which allows one or more functions to be attached to the named events, emitted by the object. Whenever the EventEmitter object emits an event all the functions attached to the specific event are called synchronously.
You can read more about “event” in built Node.js API, here.

Let’s start with a simple example.
Example 1

In this example, first we load the “events” module, using the require method. In the next line, we generate an instance of EventEmitter class. Further, we create a Fun function and bind this function on the “Hello” event, using the “on” method of EventEmitter class. It means, when “Hello” event will fire, “Fun” function will call. In the last line, we Emit the “Hello” event and in the form of output, “Fun” function is called and “Hello! C#-Corner” message is printed.
Example 2

This example is similar to the previous example, but in this example, we bind three functions for a single event, so all these functions will call synchronously.
Example 3

In this example, we create a set interval of 1000ms. After each 1000ms, “hello” event will fire and all the functions attached to this event will execute.
Passing Arguments to Listeners

In this example, we create a CsharpCorner method, which takes two arguments and binds this function to “info” event of eventEmitter. Thus, when we emit info event, we need to pass the two arguments; i.e., name and city.
Inherit EventEmitter Class
This is the common practice in Node.js to inherit the prototype of EventEmitter class. Let’s take an example and then understand this concept.
Example
Output

In this example, we create a function constructor(Sum) and in this function constructor, we implement the “call” method of “EventEmitter” class. This method is used to call a method of an object, substituting another object for the current object.
You can check the definition of the call method in “lib.es6.d.ts” file.

In next lines, we add 4 methods (Add,Mul,Sub and Div) and these methods will be used to emit the event.
“Sum.prototype.__proto__ = events.EventEmitter.prototype”
You can read more about “event” in built Node.js API, here.

Let’s start with a simple example.
Example 1

In this example, first we load the “events” module, using the require method. In the next line, we generate an instance of EventEmitter class. Further, we create a Fun function and bind this function on the “Hello” event, using the “on” method of EventEmitter class. It means, when “Hello” event will fire, “Fun” function will call. In the last line, we Emit the “Hello” event and in the form of output, “Fun” function is called and “Hello! C#-Corner” message is printed.
Example 2

This example is similar to the previous example, but in this example, we bind three functions for a single event, so all these functions will call synchronously.
Example 3

In this example, we create a set interval of 1000ms. After each 1000ms, “hello” event will fire and all the functions attached to this event will execute.
Passing Arguments to Listeners

In this example, we create a CsharpCorner method, which takes two arguments and binds this function to “info” event of eventEmitter. Thus, when we emit info event, we need to pass the two arguments; i.e., name and city.
Inherit EventEmitter Class
This is the common practice in Node.js to inherit the prototype of EventEmitter class. Let’s take an example and then understand this concept.
Example
- var events = require('events');
- function Sum() {
- events.EventEmitter.call(this);
- this.Add = function(a, b) {
- this.emit('Add', a, b);
- };
- this.Sub = function(a, b) {
- this.emit('Sub', a, b);
- };
- this.Mul = function(a, b) {
- this.emit('Mul', a, b);
- };
- this.Div = function(a, b) {
- this.emit('Div', a, b);
- };
- }
- Sum.prototype.__proto__ = events.EventEmitter.prototype;
- var Obj = new Sum();
- Obj.on('Add', function(a, b) {
- console.log('Sum IS=' + (a + b));
- });
- Obj.on('Sub', function(a, b) {
- console.log('Sub IS=' + (a - b));
- });
- Obj.on('Mul', function(a, b) {
- console.log('Mul IS=' + (a * b));
- });
- Obj.on('Div', function(a, b) {
- console.log('Div IS=' + (a / b));
- });
- Obj.Add(10, 20);
- Obj.Sub(40, 20);
- Obj.Mul(20, 50);
- Obj.Div(200, 50);

In this example, we create a function constructor(Sum) and in this function constructor, we implement the “call” method of “EventEmitter” class. This method is used to call a method of an object, substituting another object for the current object.
You can check the definition of the call method in “lib.es6.d.ts” file.

In next lines, we add 4 methods (Add,Mul,Sub and Div) and these methods will be used to emit the event.
“Sum.prototype.__proto__ = events.EventEmitter.prototype”











SubashPosted Aug 27, 2016, 3:59 AM
Good
Anu VPosted Aug 20, 2016, 1:08 AM
Nice...
Vignesh ManiPosted Aug 18, 2016, 8:37 AM
Nice
Ramesh PalaniappanPosted Aug 18, 2016, 5:16 AM
Good One