codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🟢

Node.js

8 / 63 topics
6Node.js File System7HTTP Module8Node.js Events9Streams10Buffers11Path Module12OS Module
Tutorials/Node.js/Node.js Events
🟢Node.js

Node.js Events

Updated 2026-05-15
10 min read

Node.js Events

Introduction

Node.js is built on an event-driven architecture. This means that it uses events to communicate between different parts of the application. Understanding how to use and create events in Node.js is crucial for building efficient and scalable applications.

In this tutorial, we will explore what events are in Node.js, how they work, and how you can use them effectively in your applications.

Concept

Events in Node.js are a way to handle asynchronous operations. They allow you to define functions that will be executed when a specific event occurs. The EventEmitter class is at the core of Node.js's event handling mechanism. It provides methods like on, emit, and removeListener to manage events.

Key Concepts

  1. EventEmitter: This is the class responsible for emitting and listening to events.
  2. Listeners: Functions that are executed when an event is emitted.
  3. Emitting Events: Triggering an event so that all attached listeners are called.
  4. Listening for Events: Attaching a listener function to an event.

Basic Usage

To use events in Node.js, you first need to create an instance of the EventEmitter class and then define listeners for specific events. Here's a simple example:

JavaScript
1const EventEmitter = require('events');
2
3// Create an instance of EventEmitter
4const myEmitter = new EventEmitter();
5
6// Define a listener function
7function myListener() {
8console.log('Event occurred!');
9}
10
11// Attach the listener to an event
12myEmitter.on('myEvent', myListener);
13
14// Emit the event
15myEmitter.emit('myEvent');

In this example, we create an EventEmitter instance and define a listener function myListener. We attach this listener to the 'myEvent' event using the on method. When we emit the 'myEvent' event using the emit method, the listener is executed, and "Event occurred!" is printed to the console.

Passing Arguments

You can also pass arguments to your listeners when emitting an event:

JavaScript
1const EventEmitter = require('events');
2
3// Create an instance of EventEmitter
4const myEmitter = new EventEmitter();
5
6// Define a listener function that accepts arguments
7function myListener(arg1, arg2) {
8console.log(`Arguments: ${arg1}, ${arg2}`);
9}
10
11// Attach the listener to an event
12myEmitter.on('myEvent', myListener);
13
14// Emit the event with arguments
15myEmitter.emit('myEvent', 'Hello', 'World');

In this example, when we emit 'myEvent' with the arguments 'Hello' and 'World', the listener function receives these arguments and prints them.

Removing Listeners

You can also remove listeners from an event using the removeListener method:

JavaScript
1const EventEmitter = require('events');
2
3// Create an instance of EventEmitter
4const myEmitter = new EventEmitter();
5
6// Define a listener function
7function myListener() {
8console.log('Event occurred!');
9}
10
11// Attach the listener to an event
12myEmitter.on('myEvent', myListener);
13
14// Emit the event
15myEmitter.emit('myEvent'); // Outputs: Event occurred!
16
17// Remove the listener
18myEmitter.removeListener('myEvent', myListener);
19
20// Emit the event again
21myEmitter.emit('myEvent'); // No output, since the listener has been removed

In this example, after removing the listener with removeListener, emitting 'myEvent' does not trigger any function.

Examples

Example 1: File System Events

Node.js's built-in fs module emits events when file operations are performed. Here's an example of how to listen for these events:

JavaScript
1const fs = require('fs');
2
3// Create a writable stream
4const writerStream = fs.createWriteStream('output.txt');
5
6// Listen for the 'finish' event
7writerStream.on('finish', () => {
8console.log('File write completed.');
9});
10
11// Write data to the file and close the stream
12writerStream.write('Hello, World!');
13writerStream.end();

In this example, we create a writable stream using fs.createWriteStream and listen for the 'finish' event, which is emitted when the write operation completes.

Example 2: Custom Events

You can also create custom events by extending the EventEmitter class:

JavaScript
1const EventEmitter = require('events');
2
3// Create a custom class that extends EventEmitter
4class MyEmitter extends EventEmitter {
5constructor() {
6 super();
7}
8
9// Method to trigger an event
10triggerEvent(data) {
11 this.emit('customEvent', data);
12}
13}
14
15// Create an instance of MyEmitter
16const myEmitter = new MyEmitter();
17
18// Define a listener function for the custom event
19function customListener(data) {
20console.log(`Custom event occurred with data: ${data}`);
21}
22
23// Attach the listener to the custom event
24myEmitter.on('customEvent', customListener);
25
26// Trigger the custom event
27myEmitter.triggerEvent('Hello, Custom Event!');

In this example, we create a custom class MyEmitter that extends EventEmitter. We define a method triggerEvent to emit a 'customEvent', and we attach a listener function to handle this event.

What's Next?

Now that you have a good understanding of events in Node.js, the next topic to explore is Streams. Streams allow you to read from and write to files, network sockets, and other data sources in a streaming manner, which can be very efficient for handling large amounts of data.

Stay tuned for more tutorials on Node.js core modules!


PreviousHTTP ModuleNext Streams

Recommended Gear

HTTP ModuleStreams