Node Events Module Notes

The Node.js Events module is a core module in Node.js that provides an easy way to work with events, allowing you to create, emit, and listen for events in your applications.

Introduction to Events:

  1. Events are a way for objects to signal that something has happened.
  2. In Node.js, the Events module provides a set of classes and methods for working with events.
  3. Events are widely used in Node.js for building event-driven applications like web servers, file system operations, and more.

Real Time applications of Events

  1. Real-time chat applications
  2. Live streaming applications
  3. Online gaming applications
  4. Real Time dashboards and monitoring applications

EventEmitter Class:

  1. The core of the Events module is the EventEmitter class.
  2. You need to create an instance of EventEmitter to work with events.
const EventEmitter = require('events');
const emitter = new EventEmitter();
    

Event Emitter Methods

  1. on(eventName, listener) - Adds a listener function to the specified event.
  2. once(eventName, listener) - Adds a one-time listener for the event. It's removed automatically after being called once.
  3. emit(eventName, [args]) - Emits the specified event with optional arguments.
  4. removeListener(eventName, listener) - Removes a specific listener from the event.
  5. removeAllListeners([eventName]) - Removes all listeners for a specific event or all events.

Event Listeners

  1. Event listeners are functions that are executed when an event is emitted.
  2. Listeners are associated with events using the on or once method.once
  3. You can have multiple listeners for a single event.

Creating and Emitting Events

  1. Let's create an event and emit it
emitter.on('greet', () => {
    console.log('Hello, world!');
});

emitter.emit('greet');

//Output: Hello, world!

  

Handling Events with Callbacks

  1. You can pass data to event listeners as arguments:
emitter.on('sum', (a, b) => {
    console.log(`Sum: ${a + b}`);
});

emitter.emit('sum', 3, 5);
//Output: Sum: 8
  

Once Event Listeners

  1. Once listeners are removed after being called:
emitter.once('onceEvent', () => {
    console.log('This will only happen once.');
  });
  
emitter.emit('onceEvent');
emitter.emit('onceEvent'); // This won't trigger the listener.
//Output: This will only happen once.

  

Removing Event Listeners

  1. You can remove specific event listeners
function myListener() {
    console.log('Listener 1');
  }
  
emitter.on('removeListenerExample', myListener);
emitter.emit('removeListenerExample');
emitter.removeListener('removeListenerExample', myListener);
emitter.emit('removeListenerExample'); // This won't trigger myListener.
  

Error Events

  1. In Node.js, unhandled errors can cause the program to crash. To handle errors, you can use the 'error' event
emitter.on('error', (err) => {
    console.error('An error occurred:', err.message);
});

emitter.emit('error', new Error('Something went wrong.'));

//Output: An error occurred: Something went wrong.