Event Emitter
Master Node.js EventEmitter - understand event-driven architecture, publish-subscribe pattern, and how to create and handle custom events
What is EventEmitter?
In Nodejs there is a special system called EventEmitter. It helps your program listen for something to happen and do something when it happens.
- One part emits (sends) an event
- Another part listens and reacts when that event happens
Why it is Used
Node.js is built on event-driven architecture. That means it reacts to events all the time.
Examples of events:
- When a file finishes reading
- When a user clicks something
- When data comes from a network
EventEmitter is the core of how Node.js handles many things.
Interview Answer
EventEmitter is a class in Node.js that helps create and handle custom events.
Key points:
- It works on the publish and subscribe model
- One part emits an event
- Other parts subscribe or listen to that event
- You use
onto listen andemitto trigger - It helps write clean and modular code
Worker Thread
Learn Node.js Worker Threads - run CPU-intensive tasks in parallel threads without blocking the main event loop, perfect for heavy computations
Cluster
Scale Node.js applications using the Cluster module - utilize all CPU cores, create worker processes, and improve performance with load distribution