Events Module and EventEmitter in Node.js — a practical guide to Node.js EventEmitter with clear examples you can reuse in real projects.
Events Module and EventEmitter in Node.js — 16/55 in the Node.js series. Full playlist companion: Node.js complete course on YouTube. Prefer one article? Read the complete Node.js tutorial.
Short description
Many Node APIs are event-based. `EventEmitter` lets you emit named events and attach listeners with `.on()`.
Custom emitter
const EventEmitter = require('events');
const bus = new EventEmitter();
bus.on('user:created', (user) => {
console.log('Welcome', user.name);
});
bus.emit('user:created', { name: 'Ravi' });