The Node.js core APIs use an async event-driven method, utilizing the events module for event handling via the EventEmitter class. Events use a publish/subscribe pattern to process results as they arrive, perhaps delivering partial results before encountering errors, as opposed to callbacks' request/reply pattern, which returns either a result or an error.
Node.js includes a built-in module named "Events", which allows you to create, fire, and listen for your own events. To include the built-in Events module, use the require() method. In addition, all event attributes and methods are instances of the EventEmitter object.
Event Emitters are objects that create events. All objects that emit events are instances of the EventEmitter class. Event Emitters follow the publish/subscribe pattern. The EventEmitter class has Listeners for listing events and Emitters for emitting them.
Event listeners are functions that are called when a specific event occurs. The on() method allows us to register event listeners.
Event emitters are used to manage events. We can design our event emitters by extending the EventEmitter class.
To trigger an event, we use the emit() method of the EventEmitter object. When an event is sent out, all registered listeners for that event are executed.
When we send an event, we can pass data as arguments. Event listeners can access these arguments.
Node.js has a specific event named "error" that handles errors that arise during asynchronous processes. To avoid the application from crashing, errors must be handled properly.
Node.js core modules and third-party libraries make considerable use of events. For example, the http module generates events like request and response' to handle HTTP requests and responses.
In addition to built-in events, we can create and send custom events within our apps to handle specific circumstances or interactions.