Wednesday 3 October 2018

EventEmitter: asynchronous and synchronous way of calling listeners

You can attach any number of listeners to an event and the listeners are called in the order they registered.

HelloWorld.js
var events = require('events');

var emitter = new events.EventEmitter();

/* Register an event name 'myCustomEvent' */
emitter.on('myCustomEvent', function(eventId, message) {
    console.log(`Listener1: ${eventId} : ${message}`);
});

emitter.addListener('myCustomEvent', (eventId, message) =>{
 console.log(`Listener2: ${eventId} : ${message}`);
 
});

emitter.addListener('myCustomEvent', (eventId, message) =>{
 console.log(`Listener3: ${eventId} : ${message}`);
 
});

/* Emit myCustomEvent */
emitter.emit('myCustomEvent', "demoEvent123", "Demo Application event");


For example, in the above application, I added three listeners to the event ‘myCustomEvent’, when the emitter emits the event, these listeners are called in the order they registered.

Output
Listener1: demoEvent123 : Demo Application event
Listener2: demoEvent123 : Demo Application event
Listener3: demoEvent123 : Demo Application event

Can I call listener functions asynchronously?
Yes, you can call listener functions asynchronously by switching to an asynchronous mode of operation using the setImmediate() or process.nextTick() methods.

Using setImmediate method
HelloWorld.js

var events = require('events');

var emitter = new events.EventEmitter();

/* Register an event name 'myCustomEvent' */
emitter.on('myCustomEvent', function(eventId, message) {
    setImmediate(() => {
        console.log(`Listener1: ${eventId} : ${message}`);
    });
});

emitter.addListener('myCustomEvent', (eventId, message) => {
    console.log(`Listener2: ${eventId} : ${message}`);
});

emitter.addListener('myCustomEvent', (eventId, message) => {
    setImmediate(() => {
        console.log(`Listener3: ${eventId} : ${message}`);
    });
});

emitter.addListener('myCustomEvent', (eventId, message) => {
    console.log(`Listener4: ${eventId} : ${message}`);
});

emitter.addListener('myCustomEvent', (eventId, message) => {
    setImmediate(() => {
        console.log(`Listener5: ${eventId} : ${message}`);
    });
});

/* Emit myCustomEvent */
emitter.emit('myCustomEvent', "demoEvent123", "Demo Application event");

As you see above snippet, I made listener 1, 3 and 5 asynchronous. When I ran above application, I seen below output.

Listener2: demoEvent123 : Demo Application event
Listener4: demoEvent123 : Demo Application event
Listener1: demoEvent123 : Demo Application event
Listener3: demoEvent123 : Demo Application event
Listener5: demoEvent123 : Demo Application event

Using process.nextTick method

HelloWorld.js

var events = require('events');

var emitter = new events.EventEmitter();

/* Register an event name 'myCustomEvent' */
emitter.on('myCustomEvent', function(eventId, message) {
    process.nextTick(() => {
        console.log(`Listener1: ${eventId} : ${message}`);
    });
});

emitter.addListener('myCustomEvent', (eventId, message) => {
    console.log(`Listener2: ${eventId} : ${message}`);
});

emitter.addListener('myCustomEvent', (eventId, message) => {
    process.nextTick(() => {
        console.log(`Listener3: ${eventId} : ${message}`);
    });
});

emitter.addListener('myCustomEvent', (eventId, message) => {
    console.log(`Listener4: ${eventId} : ${message}`);
});

emitter.addListener('myCustomEvent', (eventId, message) => {
    process.nextTick(() => {
        console.log(`Listener5: ${eventId} : ${message}`);
    });
});

/* Emit myCustomEvent */
emitter.emit('myCustomEvent', "demoEvent123", "Demo Application event");

As you see above snippet, I made listener 1, 3 and 5 asynchronous. When I ran above application, I seen below output.

Listener2: demoEvent123 : Demo Application event
Listener4: demoEvent123 : Demo Application event
Listener1: demoEvent123 : Demo Application event
Listener3: demoEvent123 : Demo Application event
Listener5: demoEvent123 : Demo Application event



Previous                                                 Next                                                 Home

No comments:

Post a Comment