Loading lesson path
Concept visual
Core Concepts of Events in Node.js Every action on a computer is an event, like when a connection is made or a file is opened. Objects in Node.js can fire events, like the readStream object fires events when opening and closing a file:
let fs = require('fs');
let rs = fs.createReadStream('./demofile.txt');
rs.on('open', function () {
console.log('The file is open');
});Getting Started with Events in Node.js Node.js uses an event-driven architecture where objects called "emitters" emit named events that cause function objects ("listeners") to be called.
// Import the events module const EventEmitter = require('events');
// Create an event emitter instance const myEmitter = new EventEmitter();
// Register an event listener myEmitter.on('greet', () => {
console.log('Hello there!');
});
// Emit the event myEmitter.emit('greet'); // Outputs: Hello there!Formula
EventEmitter class is fundamental to Node.js's event - driven architecture.It provides the ability to create and handle custom events.
To use the EventEmitter, you need to create an instance of it:
let events = require('events');
let eventEmitter = new events.EventEmitter();You can assign event handlers to your own events with the EventEmitter object. In the example below we have created a function that will be executed when a "scream" event is fired. To fire an event, use the emit() method.
let events = require('events');
let eventEmitter = new events.EventEmitter();//Create an event handler:
let myEventHandler = function () {
console.log('I hear a scream!');
}//Assign the event handler to an event:
eventEmitter.on('scream', myEventHandler);
//Fire the 'scream' event:
eventEmitter.emit('scream');const EventEmitter = require('events');
const emitter = new EventEmitter();
// Emit event with arguments emitter.on('userJoined', (username, userId) => {
console.log(`${username} (${userId}) has joined the chat`);
});
emitter.emit('userJoined', 'JohnDoe', 42);
// Outputs: JohnDoe (42) has joined the chatconst EventEmitter = require('events');
const emitter = new EventEmitter();
// This listener will be called only once emitter.once('connection', () => {
console.log('First connection established');
});
emitter.emit('connection'); // This will trigger the listener emitter.emit('connection'); // This won't trigger the listener againconst EventEmitter = require('events');
const emitter = new EventEmitter();
// Always handle 'error' events emitter.on('error', (err) => {
console.error('An error occurred:', err.message);
});
// This will trigger the error handler emitter.emit('error', new Error('Something went wrong'));console.error('Error in event emitter:', err);
});// Instead of anonymous functions function handleData(data) {
console.log('Received data:', data);
}
myEmitter.on('data', handleData);// Add a listener const listener = () => console.log('Event occurred');
myEmitter.on('event', listener);
// Later, remove the listener when no longer needed myEmitter.off('event', listener);