AppDividend
Latest Web Development Tutorials

Node.js Events EventEmitter Tutorial Example From Scratch

427

Node.js Events EventEmitter Tutorial Example From Scratch is today’s leading topic. EventEmitter is the implementation of Node.js’s pub-sub design patterns. Node.js core API has built on an asynchronous event-driven architecture. In an asynchronous event architecture, certain kinds of objects (called “emitters”) periodically emit named events that cause Function objects (“listeners”) to be called.

 

Each object that emits events are instances of an EventEmitter class.

We are going to see one example with ES5 Syntax.

Node.js Events EventEmitter

Example #1

// ES5 Event Emitter Example

var events = require('events').EventEmitter;
var emitter = new events.EventEmitter();

emitter.on('newEvent', function(user){
    console.log(user);
});

emitter.emit('newEvent', "Krunal");

In above example, first, we are going to import events object, and then we get emitter object.

The event emitter class has two methods.

  1. On
  2. emit

So, if we make an object of EventEmitter class, then we have access to this methods.

emitter.on('newEvent', function(user){
    console.log(user);
});

Here, I have defined an event and till now, not called just determine.

On() method takes an event name and a call back function, which describes the logic and payload of the function. As we know, Node.js has the event-driven architecture, so it first occurs the events and then related to that event, one callback function is returned.

emitter.emit('newEvent', "Krunal");

Here, I have emitted the event, so related to that event, the callback function is invoked, and that function will execute. The first parameter is event name and second is payload.

Related Posts
1 of 2

The output will be following.

Krunal



Example #2

var EventEmitter = require('events').EventEmitter;
var util = require('util');

var User = function(username){
    this.username = username;
}
util.inherits(User, EventEmitter);
var user = new User('Krunal Lathiya');
user.on('nuevent', function(props){
    console.log(props);
});
user.emit('nuevent', 'dancing');

In this example, first I have created an EventEmitter object and then also create User function constructor.



Then, import the Node.js’s core module util and inherits the base functionality from EventEmitter module to the newly created User module.

So, now User has all the methods and properties of the EventEmitter module, and we can use two methods on it.

Now, user object’s behavior is same as EventEmitter, and we can define events on it and emit the events.

The output will be “dancing.”

Example #3

const EventEmitter = require('events');

class MyEmitter extends EventEmitter {}

const myEmitter = new MyEmitter();
myEmitter.on('event', () => {
  console.log('an event occurred!');
});
myEmitter.emit('event');

Above is an ES6 example of Node.js events and event emitter.

First, we have included the EventEmitter class, and then we define our class and extends the base EventEmitter class.

Now, make an object of the newly created class, so we have access all the methods of the EventEmitter class. Rest is all the same.

 

Leave A Reply

Your email address will not be published.