In Node.js, The Events module provides a simple and efficient mechanism for handling emitting events.
Events in Node.js allow eventEmiiter objects to emit named events that can be listened to by event listeners functions.
This pattern is commonly used for building asynchronous, event-driven applications.
Events module allows us to create custom events and event listeners, enabling communication between different parts of our application.
An instance of EventEmitter is created using "new EventEmitter()".
An event listener is registered for the event named 'custom-event' using `on()` method.
The "emit()" method is used to trigger events such as 'custom-event', causing the registered event listener to be executed.
let http = require("http"); const EventEmitter = require('events'); // Create an EventEmitter instance const yourEventEmitter = new EventEmitter(); // Define an event listener for the 'custom-event' event yourEventEmitter.on('custom-event', () => { console.log(`Custom Event occurred!`); }); // Emit the 'event' event yourEventEmitter.emit('custom-event'); http .createServer(function (req, res) { res.writeHead(200, { "Content-Type": "text/html" }); res.end("Server Start Listening on 3002"); }) .listen(3002);
Custom Event occurred!
We can pass arguments along with events and we can access arguments or payloads from the callback function of events.
let http = require("http"); const EventEmitter = require('events'); // Create an EventEmitter instance const yourEventEmitter = new EventEmitter(); // Define an event listener for the 'custom-event' event yourEventEmitter.on('custom-event', (payload) => { console.log(`Custom Event occurred!: Hello, My Name is ${payload.name}`); }); // Emit the 'event' event yourEventEmitter.emit('custom-event', { name: 'Alice'}); http .createServer(function (req, res) { res.writeHead(200, { "Content-Type": "text/html" }); res.end("Server Start Listening on 3002"); }) .listen(3002);
Custom Event occurred!: Hello, My Name is Alice
we can restrict an event listener to be called only once, we can use the "once()" method:
let http = require("http"); const EventEmitter = require('events'); const yourEventEmitter = new EventEmitter(); yourEventEmitter.once('onceEvent', () => { console.log('It will trigger this event only once.'); }); yourEventEmitter.emit('onceEvent'); yourEventEmitter.emit('onceEvent'); // This won't trigger the event again http .createServer(function (req, res) { res.writeHead(200, { "Content-Type": "text/html" }); res.end("Server Start Listening on 3002"); }) .listen(3002);
It will trigger this event only once.
We can create a custom class that extends "EventEmitter".
let http = require("http"); const EventEmitter = require('events'); class YourCustomEmitterClass extends EventEmitter { // methods or properties can be added inside class } const yourCustomEmitterClassInstance = new YourCustomEmitterClass(); yourCustomEmitterClassInstance.on('customEvent', () => { console.log('Custom event handler class.'); }); yourCustomEmitterClassInstance.emit('customEvent'); http .createServer(function (req, res) { res.writeHead(200, { "Content-Type": "text/html" }); res.end("Server Start Listening on 3002"); }) .listen(3002);
Custom event handler class.