MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
EventEmitter Pattern & Custom Event Architecture0%
Study Materials
Node.js & Express.jsProgress 0%
1.1Node.js Architecture, V8 Engine & Single-Threaded Event Loop1.2Call Stack, Callback Queue, Microtask Queue & Libuv Workers1.3Non-Blocking Asynchronous Code with Promises and Async/Await
2.1CommonJS vs ES Modules & Package.json Dependency Management2.2File System Operations: Sync, Async & Promises API (fs/promises)2.3EventEmitter Pattern & Custom Event Architecture2.4Buffers and Streams: Readable, Writable & Pipeline for Large Files
HomeTutorialsNode.js & Express.jsEventEmitter Pattern & Custom Event Architecture
Buffers and Streams: Readable, Writable & Pipeline for Large Files

EventEmitter Pattern & Custom Event Architecture

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

Node.js is fundamentally an event-driven platform. Much of the Node.js core API (including HTTP servers, streams, and file system watchers) is built around the EventEmitter class located in the built-in node:events module.


1. What is an EventEmitter?

An EventEmitter implements the Observer Design Pattern. Objects (called emitters) emit named events that cause previously registered functions (listeners) to be called synchronously.

1import { EventEmitter } from 'node:events';
2 
3// Instantiate emitter
4const userEmitter = new EventEmitter();
5 
6// Register listener for 'userRegistered' event
7userEmitter.on('userRegistered', (user) => {
8 console.log(`[Email Service] Welcome email sent to: ${user.email}`);
9});
10 
11// Register another independent listener for the SAME event
12userEmitter.on('userRegistered', (user) => {
13 console.log(`[Analytics Service] Logged user registration event for ID: ${user.id}`);
14});
15 
16// Trigger / Emit the event with payload
17userEmitter.emit('userRegistered', { id: 'usr_991', email: 'alex@example.com' });

2. Key Methods of EventEmitter

MethodDescription
emitter.on(eventName, listener)Registers a persistent listener that triggers every time the event fires
emitter.once(eventName, listener)Registers a one-time listener that automatically removes itself after its first execution
emitter.emit(eventName, ...args)Synchronously calls each listener registered for the event with passed arguments
emitter.removeListener(event, fn)Unbinds a specific callback from an event
emitter.setMaxListeners(n)Increases warning threshold for listener count (default is 10 to prevent memory leaks)

3. Real-World Architecture: Custom Domain Emitters

In scalable enterprise architectures, custom services inherit from EventEmitter to decouple complex domain processes:

1import { EventEmitter } from 'node:events';
2 
3class PaymentGateway extends EventEmitter {
4 async processPayment(transactionId, amount) {
5 this.emit('paymentStarted', { transactionId, amount });
6 
7 try {
8 // Simulate payment processing
9 await new Promise(resolve => setTimeout(resolve, 500));
10
11 this.emit('paymentSuccess', {
12 transactionId,
13 amount,
14 settledAt: new Date()
15 });
16 } catch (err) {
17 this.emit('paymentFailed', { transactionId, error: err.message });
18 }
19 }
20}
21 
22// Consuming the decoupled domain service
23const payments = new PaymentGateway();
24 
25payments.on('paymentSuccess', (data) => {
26 console.log('Generating PDF receipt for transaction:', data.transactionId);
27});
28 
29payments.on('paymentFailed', (data) => {
30 console.warn('Alerting risk operations team:', data.error);
31});
32 
33payments.processPayment('tx_88319', 499.00);

Interactive Quiz• Knowledge Check

Multiple Choice Questions

Question 1
Which core design pattern is implemented by the Node.js EventEmitter class?
Question 2
What happens if you register an event listener using emitter.once() instead of emitter.on()?
Question 3
What is the default maximum number of listeners allowed on an EventEmitter instance before Node.js prints a memory leak warning?
Question 4
How are listeners executed when emitter.emit('eventName') is invoked?
Question 5
What happens if an error event is emitted on an EventEmitter and no listener is registered for 'error'?

Next Lesson

Buffers and Streams: Readable, Writable & Pipeline for Large Files

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
File System Operations: Sync, Async & Promises API (fs/promises)Buffers and Streams: Readable, Writable & Pipeline for Large Files

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master Node.js & Express.js with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext