Web
Analytics
event emitters node js | example | Angular | ASP.NET Tutorials

For Consultation : +91 9887575540

Stay Connected :

What is Event and Event Emitter in Node JS

  • An User Action i.e click
  • Event names are defined simply by calling .emit with the event name.
  • An object sends events using the .emit function.
  • Events are sent to any listeners that have registered to receive events from the object.
  • The program registers to receive an event by calling that object’s .on method, giving the event name and an event handler function.

How to Send Data using Emit Method

  • it is required to send data along with an event. To do so, simply add the data as arguments to the .emit call
  • this.emit(‘eventName’, data1, data2, ..);





 

Example

var events = require('events');
var eventEmitter = new events.EventEmitter();
var myfun = function (data) {
    console.log('Something happen!' +data );
}
eventEmitter.on('Hello', myfun);
eventEmitter.emit('Hello','this is my data');

Output

Node JS event emitter example