Understanding EventEmitter in Node.js With a UseCase
Tags: showdev, webdev, tutorial, beginners
Published: 2019-08-24 09:34:04

---
title: Understanding EventEmitter in Node.js With a UseCase
published: true
description: Understanding EventEmitter in Node.js With a UseCase
tags: showdev,webdev,tutorial,beginners
canonical_url : https://cloudnweb.dev/2019/08/understanding-eventemitter-in-node-js-with-a-usecase/
---

<!-- wp:paragraph -->
<p>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</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Recent Articles about Node.js,</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><a href="https://cloudnweb.dev/2019/08/apache-kafka-for-node-js-developers/">Apache Kafka for Node.js Developers</a></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>One of the core concepts of Node.js is <a href="https://nodejs.org/api/events.html">events</a>. Event Emitter plays a vital role in Node.js Event-Driven Architecture</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p> That is to say, we will learn Event Emitter by building a custom logger using Event Emitter in this article.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>How does it work?</h3>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li>Event Emitter emits the data in an event called <strong>message</strong></li><li>A Listened is registered on the event <strong>message</strong></li><li>when the <strong>message </strong>event emits some data, the listener will get the data.</li></ul>
<!-- /wp:list -->

<!-- wp:image {"id":1273} -->
<figure class="wp-block-image"><img src="https://cloudnweb.dev/wp-content/uploads/2019/08/eventEmitter_works.png" alt="" class="wp-image-1273"/></figure>
<!-- /wp:image -->

<!-- wp:heading {"level":3} -->
<h3>Building Blocks</h3>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><li><strong>.emit()</strong> - this method in event emitter is to emit an event in module</li><li><strong>.on()</strong> - this method is to listen to data on a registered event in node.js</li><li><strong>.once()</strong> - it listen to data on a registered event only once.</li><li>.<strong>addListener</strong>() - it checks if the listener is registered for an event.</li><li>.<strong>removeListener</strong>() -  it removes the listener for an event.</li></ul>
<!-- /wp:list -->

<!-- wp:image {"id":1274} -->
<figure class="wp-block-image"><img src="https://cloudnweb.dev/wp-content/uploads/2019/08/eventemiitter.png" alt="" class="wp-image-1274"/></figure>
<!-- /wp:image -->

<!-- wp:code -->
<pre class="wp-block-code"><code>const EventEmitter = require('events');

const emitter = new EventEmitter();

emitter.on('message',function(message) {

  console.log("subsribed",message);
})

emitter.emit('message',{ data : "Data" });</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Firstly, import the package called <strong>events</strong> which is basically a class. After that, instantiates the class with a name <strong>emitter</strong>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>After that, we need to register the listener for the particular event.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Finally, when if we emit an event, the registered listener will get the event along with the data.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Use-Case</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>As I said earlier, event emitter is an important concept in node.js. we learned how it works and important building blocks of it.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>But, One of the important questions that come to our mind when we learn something is, where do I need to implement that?.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Let's see a use-case to understand more about Event-Emitters. </p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Logger using Event Emitter</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Mainly, we will see how to write a logger function using Event Emitter.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>create a file called <strong>logger.js</strong> and add the following code.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>const EventEmitter = require('events');

class Logger extends EventEmitter {

   logIt(eventName,message) {
      
      this.emit(eventName,message);
   }
}

module.exports = Logger</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>class <strong>Logger </strong>extends the <strong>EventEmitter</strong> class. add the following method called <strong>logIt</strong> which takes event name and message as an argument.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>create a file called an <strong>index.js</strong> and add the following code.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><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 })</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>it requires the module  <strong>Logger</strong> and instantiates the class as a <strong>logger</strong>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>logIt()</strong> function emits event called <strong>logMessage</strong>. the listener will listen for the event and log it.</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<iframe height="400px" width="100%" src="https://repl.it/@ganeshmani/EventEmitterNode?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe>
<!-- /wp:html -->

<!-- wp:paragraph -->
<p>To Learn some Advanced concepts in Node.js, One of my favorite courses is from <a href="https://www.udemy.com/user/sgslo/">Stephen Grider</a>. I recommend this course to learn advanced concepts in Node.js</p>
<!-- /wp:paragraph -->

<!-- wp:html -->
<a href="https://click.linksynergy.com/link?id=YIJ5oggSvpI&offerid=507388.1587718&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Fadvanced-node-for-developers%2F">Node JS: Advanced Concepts</a><IMG border=0 width=1 height=1 src="https://ad.linksynergy.com/fs-bin/show?id=YIJ5oggSvpI&bids=507388.1587718&type=2&subid=0" >
<!-- /wp:html -->