Getting Started with Node.js Event Emitter

Node.js has an asynchronous event-driven architecture. This allows designs where events emitted due to an action can cause listener object(s) to be executed. Node.js has a built-in module events. This module has an object Event Emitter which we can manipulate to listen to events.

1. Prerequisites

This article assumes you have basic knowledge in JavaScriptREPL, and Node.js installed in your local development environment.

2. Event emitter object

The Event Emitter object lies within the events module. It has an eventEmitter.on() method which it exposes to allow for function(s) to be attached to emitted events.

Let’s begin by importing the events module:

~$ node
Welcome to Node.js v15.12.0.
Type ".help" for more information.
> const events = require('events');
undefined
> const eventEmitter = new events.EventEmitter();
undefined
> 

In the scripts above, we first import the events module. We then create an instance of the Event Emitter class.
Alternatively, we can import the events module and extend the main class with our custom myEventEmitter class as seen below:

$ node
Welcome to Node.js v15.12.0.
Type ".help" for more information.
> const EventEmitter = require('events');
undefined
> class MyEventEmitter extends EventEmitter {}
undefined
> const myEventEmitter = new MyEmitter();
undefined
> myEventEmitter.on('event', () => {
...   console.log('an event emitted!');
... });

> myEventEmitter.emit('event');

It’s important to note that any event emitting object in Node.js is a member of the Event Emitter class.

3. Emitting events

The idea of events in Node.js is quite straightforward. Event emitting object emit named events. These events cause the listeners previously registered to be called.

Let’s look at an event emitting example:

$ node
Welcome to Node.js v15.12.0.
Type ".help" for more information.
> const eventEmitter = require(`events`);
undefined
> class MyEventEmitter extends eventEmitter{}
undefined
> const myEmitter = new MyEventEmitter();
undefined
> function myFirstEvent(){
... console.log(`my first event occurred`);
... }
undefined
> function mySecondEvent(){
... console.log(`hooray, another event has occured`);
... }
undefined
> myEmitter.on('event', myFirstEvent);
MyEventEmitter {
  _events: [Object: null prototype] { event: [Function: c1] },
  _eventsCount: 1,
  _maxListeners: undefined,
  [Symbol(kCapture)]: false
}
> myEmitter.on('event', myFirstEvent);
MyEventEmitter {
  _events: [Object: null prototype] {
    event: [ [Function: myFirstEvent], [Function: myFirstEvent] ]
  },
  _eventsCount: 1,
  _maxListeners: undefined,
  [Symbol(kCapture)]: false
}
> 

When we emit the event event, myFirstEvent() and mySecondEvent() callbacks should be invoked.

Output:

--------------------------
> myEventEmitter.emit(`event`);
my first event occurred
hooray, another event has occurred
true
> 

4. Registering for the event to be fired only one time using once

Previously, we discussed that event listeners were invoked each time an event they are attached to is emitted.
There exist scenarios where we only need to execute these listeners once. In these cases, we make use of the eventEmitter.once().
Let’s take a look at an example:

--------------------
>myEventEmitter.once('MyOnceEvent', () => console.log('my once event fired')); 

Emit MyOnceEvent event:

-----------------------
> myEventEmitter.emit('MyOnceEvent');

Output:

my one event fired

Let’s try to emit this event again:

> myEventEmitter.emit('MyOnceEvent');

The event was emitted once and cannot be emitted again, resulting in a blank screen.

5. Registering for the event with callback parameters

The eventEmitter.emit() method allows the listener functions to be passed arguments. Let’s take a look at how we can achieve this functionality:

myEventEmitter.on('status', (statusCode, statusMsg)=> console.log(`Status code = ${code} while message= ${statusMsg}`));
myEventEmitter {
  _events: [Object: null prototype] { status: [Function (anonymous)] },
  _eventsCount: 1,
  _maxListeners: undefined,
  [Symbol(kCapture)]: false
}
> 

Pass parameters:

----------------
> myEventEmitter.emit('status', 201, 'created');
> 

Output:

Status code =201 while message= created

6. Error events

Error events are emitted whenever an error occurs within an EventEmitter instance. In case an eventEmitter does not have registered error events, an error event will be emitted, exiting the Node.js process.

Let’s look at an example:

---------------------------------------------
> myEmitter.emit('error', new Error('whoops, an error instance!'));
Uncaught [Error: an error instance!] {
  domainEmitter: MyEventEmitter {
    _events: [Object: null prototype] {},
    _eventsCount: 0,
    _maxListeners: undefined,
    [Symbol(kCapture)]: false
  },
  domainThrown: false
}
> false
> 

You will notice that an error is thrown since we don have any listener for the error.

7. Unregistering events

Now that we’ve seen how we can create events, what if we need to unregister them? To unregister the event we created previously, we call the eventEmitter.off() method and pass it to the event as seen below:

myEmitter.off('event', myFirstEvent); // if you try emitting this event, nothing happens

8. Conclustion

In this tutorial, we discussed the EventEmitter object. Using this object, we were able to create an event, fire an event, and listen to it.

We’ve also briefly looked at the error event, that causes the Node.js process to crash.

Happy coding!!

Related posts:

Uploading Images to Cloudinary using Node.js
Converting A Static Site to A Dynamic Node.js Web App
Rendering HTML Pages as an HTTP Server Response Using Node.js
A Vanilla Node.js REST API without Frameworks such us Express
The Issue With Global Node Packages
Build a Ticketing App with Adonis.js and Vue.js
Speakeasy Two-factor Authentication in Node.js
Next Generation Server Compression With Brotli
Exploring Node.js Internals
Deploying RESTful APIs using Node.js, Express 4 to Kubernetes clusters
Building A Node.js Express API To Convert Markdown To HTML
Agora Cloud Recording with Node.js
The History of Node.js
Get Started With Node: An Introduction To APIs, HTTP And ES6+ JavaScript
Testing Node.js Applications
Getting Started with EJS Templating Engine
Why Node.js is Good for Online Stores
Debugging a Node.js app in VS Code
Implementing AWS S3 Functionalities on a Node.js GraphQL API
Getting Started with the Quasar Framework
How To Auto-generate Admin Panels for Node.js with AdminBro
Working with Moment.js Date Libraries
Getting Started with Node.js Paypal Checkout Integration
Building a Websocket Powered Tic-Tac-Toe Game with Adonis.js
Creating a Real Time Chat App using React and Socket IO with E2E Encryption
Node.js versus Next.js - A React Approach
Getting Started with Node.js Module
Getting Started with Google Translate API with Node.js
10 Tips for Working with Node.js
Writing A Multiplayer Text Adventure Engine In Node.js: Creating The Terminal Client (Part 3)
Choosing Between NPM and Yarn
Uploading Files Using Multer in a Node.js Application