Emitting Events in Node.js
Learn how to emit events using EventEmitter, pass data to listeners, and build practical event-driven applications with CIIT Training Institute examples.
In the previous lessons, we learned about EventEmitter and how
to register event listeners using on() and
once(). In this lesson, we will learn how to emit
events using emit() and how registered listeners
respond when an event is emitted.
We will use practical examples involving Samadhan and Pradnya, who are working with student enquiries and course admissions at CIIT Training Institute.
1. What Is Emitting an Event?
Emitting an event means notifying an EventEmitter that a
particular event has occurred. Node.js provides the
emit() method to perform this operation.
When an event is emitted, Node.js calls the listeners registered
for that event name. We can also pass additional values to
those listeners through the arguments of emit().
For example, when Samadhan adds a new student enquiry to the
CIIT Training Institute system, the application can emit a
studentEnquiryAdded event. A listener can display
a confirmation message or perform another required action.
on() registers a listener, while
emit() emits the event. The listener must be
registered for the matching event name to respond to that emission.
2. Syntax of the emit() Method
The emit() method accepts an event name followed
by zero or more arguments. These additional arguments are
passed to the registered listeners.
Basic Syntax
emitter.emit("eventName");
emitter.emit("eventName", argument1, argument2);
Understanding the Syntax
| Part | Meaning |
|---|---|
emitter |
The EventEmitter object. |
emit() |
Emits a named event. |
"eventName" |
The name of the event to emit. |
argument1 |
The first optional value passed to listeners. |
argument2 |
The second optional value passed to listeners. |
3. First Program Using emit()
Let's create a simple program that emits a
welcome event. The registered listener will display
a welcome message for CIIT Training Institute.
Step 1: Create a File
Create a JavaScript file named emittingEvents.js.
Step 2: Write the Program
const EventEmitter = require("node:events");
const emitter = new EventEmitter();
emitter.on("welcome", () => {
console.log("Welcome to CIIT Training Institute!");
});
emitter.emit("welcome");
Step 3: Run the Program
node emittingEvents.js
Step 4: Expected Output
Welcome to CIIT Training Institute!
Step 5: Understand the Code
-
require("node:events")imports the built-in EventEmitter class. -
new EventEmitter()creates an emitter object. -
emitter.on("welcome", ...)registers the welcome event listener. -
emitter.emit("welcome")emits the event and executes the registered listener.
4. How Does Event Emission Work?
This diagram explains the sequence of registering a listener, emitting an event, and executing the corresponding callback.
Step 1: Create EventEmitter
new EventEmitter()
Step 2: Register Listener
emitter.on("welcome", callback)
Step 3: Emit Event
emitter.emit("welcome")
Step 4: Execute Listener
Display the welcome message.
Note: EventEmitter listeners are normally invoked synchronously when an event is emitted. If no matching listener is registered, the emission does not invoke a callback for that event.
5. Practical Example: CIIT Student Enquiry
Imagine that Samadhan works with student enquiries at CIIT Training Institute. When a new student enquiry is added, the application should display a confirmation message.
We can create a studentEnquiryAdded event and
pass the student's name to its listener.
Program
const EventEmitter = require("node:events");
const emitter = new EventEmitter();
emitter.on("studentEnquiryAdded", (studentName) => {
console.log(
"New student enquiry received: " + studentName
);
});
emitter.emit("studentEnquiryAdded", "Samadhan");
emitter.emit("studentEnquiryAdded", "Pradnya");
Expected Output
New student enquiry received: Samadhan
New student enquiry received: Pradnya
Explanation
-
The listener is registered for
studentEnquiryAdded. -
The first emission passes
"Samadhan"to the listener. -
The second emission passes
"Pradnya"to the same listener. - The callback executes for each emission and displays the corresponding name.
6. Passing Multiple Arguments with emit()
We can pass multiple values to a listener. This is useful when an event needs to provide more than one piece of information.
For example, Pradnya registers a student for a course at CIIT Training Institute. The event can carry the student's name and course name.
Program
const EventEmitter = require("node:events");
const emitter = new EventEmitter();
emitter.on("studentRegistered", (studentName, courseName) => {
console.log("Student: " + studentName);
console.log("Course: " + courseName);
});
emitter.emit(
"studentRegistered",
"Pradnya",
"Node.js Development"
);
Expected Output
Student: Pradnya
Course: Node.js Development
Understanding the Arguments
| Emitted Value | Listener Parameter |
|---|---|
"Pradnya" |
studentName |
"Node.js Development" |
courseName |
The arguments are passed in order. The first value is received
by studentName, and the second value is received
by courseName.
7. Event Data Flow Diagram
The following diagram shows how student and course information
moves from emit() to the registered listener.
1. Emit Student Event
emit("studentRegistered", "Pradnya", "Node.js Development")
2. Pass Arguments
studentName = "Pradnya"
courseName = "Node.js Development"
3. Listener Receives Values
(studentName, courseName) => { ... }
4. Display Information
Student: Pradnya
Course: Node.js Development
8. Emitting the Same Event Multiple Times
A listener registered with on() can execute every
time the same event is emitted, as long as it remains registered.
Program
const EventEmitter = require("node:events");
const emitter = new EventEmitter();
emitter.on("courseEnquiry", () => {
console.log("New course enquiry received.");
});
emitter.emit("courseEnquiry");
emitter.emit("courseEnquiry");
emitter.emit("courseEnquiry");
Expected Output
New course enquiry received.
New course enquiry received.
New course enquiry received.
The event is emitted three times, so the registered listener executes three times. This behavior is useful when an application processes repeated actions, such as receiving multiple enquiries.
9. Understanding the Return Value of emit()
The emit() method returns a Boolean value. It
returns true when at least one listener was
registered for the specified event and false
when no listener was registered for that event.
Program
const EventEmitter = require("node:events");
const emitter = new EventEmitter();
emitter.on("courseAdded", () => {
console.log("Course added successfully.");
});
const result = emitter.emit("courseAdded");
console.log(result);
Expected Output
Course added successfully.
true
Because a listener is registered for courseAdded,
the event executes that listener and emit()
returns true.
Important Points
-
emit()is used to emit a named event. -
Register a listener with
on()before emitting an event if it must receive that emission. -
Event names used in
on()andemit()must match. -
Additional arguments passed to
emit()are received by the listener in the same order. -
A listener registered with
on()can respond to repeated emissions. -
emit()returnstrueif the event has registered listeners andfalseotherwise.
Summary
In this lesson, we learned how to emit events in Node.js
using emit(). We created event listeners,
emitted events for CIIT Training Institute examples,
passed student names and course information to callbacks,
emitted the same event multiple times, and understood
the Boolean return value of emit().
These concepts help us build event-driven applications
in which different components can respond to specific
occurrences.