Aug 24, 2019· 3 mins to read

Understanding EventEmitter in Node.js With a UseCase


Understanding EventEmitter in Node.js With a UseCase

In this article, we will see what are event emitters and how it works by understanding some use cases. Understanding EventEmitter in Node.js With a UseCase

Recent Articles about Node.js,

Apache Kafka for Node.js Developers

One of the core concepts of Node.js is events. Event Emitter plays a vital role in Node.js Event-Driven Architecture

Event Emitter is a module that facilitates communication between objects in Node.js. Most of the Libraries/Modules built on Node.js uses EventEmitter Since Node.js follows the Event-Driven Architecture.

That is to say, we will learn Event Emitter by building a custom logger using Event Emitter in this article.

How does it work?

  • Event Emitter emits the data in an event called message
  • A Listened is registered on the event message
  • when the message event emits some data, the listener will get the data.

event emitter works

Building Blocks

  • .emit() - this method in event emitter is to emit an event in module
  • .on() - this method is to listen to data on a registered event in node.js
  • .once() - it listen to data on a registered event only once.
  • .addListener() - it checks if the listener is registered for an event.
  • .removeListener() - it removes the listener for an event.

event emitter

const EventEmitter = require("events");

const emitter = new EventEmitter();

emitter.on("message", function (message) {
  console.log("subsribed", message);
});

emitter.emit("message", { data: "Data" });

Firstly, import the package called events which is basically a class. After that, instantiates the class with a name emitter.

After that, we need to register the listener for the particular event.

Finally, when if we emit an event, the registered listener will get the event along with the data.

Use-Case

As I said earlier, event emitter is an important concept in node.js. we learned how it works and important building blocks of it.

But, One of the important questions that come to our mind when we learn something is, where do I need to implement that?.

Let’s see a use-case to understand more about Event-Emitters.

Logger using Event Emitter

Mainly, we will see how to write a logger function using Event Emitter.

create a file called logger.js and add the following code.

const EventEmitter = require("events");

class Logger extends EventEmitter {
  logIt(eventName, message) {
    this.emit(eventName, message);
  }
}

module.exports = Logger;

class Logger extends the EventEmitter class. add the following method called logIt which takes event name and message as an argument.

create a file called an index.js and add the following code.

const Logger = require("./Logger");

const logger = new Logger();

logger.on("logMessage", (arg) => {
  console.log("Listened value => ", arg);
});

logger.logIt("logMessage", { name: "John", age: 40 });

it requires the module Logger and instantiates the class as a logger.

logIt() function emits event called logMessage. the listener will listen for the event and log it.

To Learn some Advanced concepts in Node.js, One of my favorite courses is from Stephen Grider. I recommend this course to learn advanced concepts in Node.js

Node JS: Advanced Concepts

Copyright © Cloudnweb. All rights reserved.