Quick Contact


    EXTEND EVENT EMITTER

    In real world it is quite rare that you would work with this event emitter object directly instead you want to create a class that has all the capabilities of event emitter then you will use that class in your code. Let’s understand it by the code.

    Let’s start with logger module i.e.-:

    	varurl = ‘’;
           Function log(message) {
              //Send an HTTP request
         Console.log (message);
       }
         Module. Exports = log;
    	

    So, here simple function log is exported and above it message is logged to console. After you log the message to console, now raise an event because after this module you will listen to the module only and then do something.

    The file to which we are exporting this log module is app.js file application as-:

    	ConstEventEmitter = require (‘events’);
    	Const emitter = new EventEmitter();
    	         //register a listener
    	Emitter.on(‘messagelogged’, (arg) => {
    	Console.log(‘listener called’, arg);
    	}
    	);
    	     //raise an event
    	Emitter.emit(‘messageLogged’, { id: 1, url: ‘http://’ });
    	   //Raise: logging (data: message)
    	

    Now as you are creating log module and for now it is used in this file so let’s copy some code from this file for your logger module file.

    So copy above two Const variables i.e.-:

    ConstEventEmitter = require (‘events’);

    Const emitter = new EventEmitter ();

    Copy these two lines to bring the event emitter in this module and also copy the lines of raising module i.e.-:

    Emitter.emit(‘messagelogged’, { id: 1, url: ‘http://’ });

    Because this code shouln’t is in app file it should be in logger module. As, Logger module that emits or signals an event saying the message is locked.

    In logger module after you log the message, raise an event.

    Now the Logger module is as-:

    	ConstEventEmitter = require (‘events’);
    	Const emitter = new evnetEmitter();
    	Varurl = ‘http://....’;
    	      Function log (message) {
    	        Console.log (message);
    	Emitter.emit(‘messageLogged’, { id: 1, url: ‘http://’ });
    	       }
    	Module.exports  = log;
    	
    Now go to app.JS file, and here you need to load logger module to access in this file and call the log function and app module is as-:
    	ConstEventEmitter = require (‘events’);
    	Const emitter = new EventEmitter ();
    	Emitter.on(‘messagelogged’, (arg) => {
    	         Console.log (‘listener called’, arg);
    	         });
    	Const log = require (‘./logger’);
    	        Log (‘message’);
    	
    Now to understand the process go to terminal and execute the file “node app.js” in terminal.
    Output is-:
    So in output you only get on the console and event listener is not caught.

    Reason for this is here you are working with two different event emitters. As in app.js you have event emitter object and logger module you have another event emitter object.

    As you already know that class is a blueprint but the object is actual instance and in app and logger there are two different emitter objects to handle the event.

    So, as you register the listener app.js then that listener is only register with event emitter in app.js only which is completely different from the other event emitter in logger module.

    In the application it’s very rare that you would want to work with this event emitter directly instead you want to create a class that has all the capabilities of this event emitter but it has additional capabilities. In this case you want to create a class called logger having additional method log.

    So define a class in logger module, in ES6 you have this keyword class which is a syntactical. Note that first letter of every word in class should be of upper case.

    So create class as-:

    Class Logger {

    }

    Now move the log function in logger module inside the logger class as-:

    	               Class Logger {
    	 Function log (message) {
    	   Console.log (message);
    	Emitter.emit(‘messageLogged’, { id: 1, url: ‘http://’ });
    	  }
    	}
    	
    So here you have an error because whenever you define a function inside class, you don’t need function keyword inside class because when there is a function in class then it define as method in class so class should be like-:
    	Class Logger {
    	Log (message) {
    	Console.log (message);
    	Emitter.emit(‘messageLogged’, { id: 1, url: ‘http://’ });
    	}
    	}
    	

    So now in the end export the log function and now export the logger class. For this logger class you have all the capabilities of this event emitter. It is done by using extends keyword that comes in ES6 so, write extends and add the name of parent or base class as-:

    Class Logger extends EventEmitter {

    }

    Now logger class will have all the functionality that is defined in event emitter. You can also directly emit or raise the events by using “this” in logger module as-:

    This.emit(‘messagelogged’, {id: 1, url: ‘http://’});

    And the code will be as-:
    extend event emitter
    So now come to another file app.js when logger module is requiring you will get a class so rename log to logger and create new object as-

    Const logger = new logger ();

    And then to log a message, call logger deadlock. Now similar the change made in logger module, you need of event emitter object in app.js. Because here also you want to work directly with the logger object above created. So for this register listener defines in respective app module on this logger object and the code will be as-:

    extend event emitter

    Now you will see after executing, the message on the console but also because you are using same logger object for registering an event listener and also raising an event, you are going to see this message on the console i.e.

    Console.log (‘Listener called’, arg);

    As-:
    Output is-:
    extend event emitter
    Summary of Event Emitter you learn above is-:

    If you want to raise events in your application to signal that something has happened you need to create a class that extend event emitter with this that class will have all the functionality defined in event emitter but you can also add additional functionality. In this case you have the ability to log a message and then inside that class.

    Copyright 1999- Ducat Creative, All rights reserved.