Quick Contact


    Event is basically a signal that happens in an application. For Example in Node, there is a class called HTTP, that we can used build web server in which port is there to response. As every time request is received on that port that HTTP class raises an event. Now the purpose is to respond to that event which basically involves reading that request and returning the right response. So, as you go through node documentation you can see several classes in node raises different kinds of events and in your code you might be interested to respond to those events so we have-:


    Now see how to work with event module?

    As there is a class known as Event Emitter. It’s one of the core building blocks of node and lot of classes based on this event emitter.

    So in following code first the events module is loaded with “require” function as-:

    Require (‘events’); from which you get the event emitter class. Take this function into variable to reuse it as-:

    ConstEventEmitter = require (‘events’);

    Note that first alphabet of each word is in uppercase that indicates that this event emitter is a class it’s not a function it’s not a simple value.

    A class is container for properties and functions which we call methods. So in following code event emitter class, so class is a container for a bunch of related methods and properties.

    So, in order to use event emitter first you need to create instance of this class, so “constant emitter” set to new event emitter as-:

    ConstEventEmitter = require (‘events’);
    Const emitter = new EventEmitter();

    So, here emitter is an object. So, if you think what is the difference between object and class? A class is like a human and an object is like an actual person. So, a class defines the properties and behaviour of a concept like a human but an object is an actual instance of that class. So this emitter has bunch of methods-:

    Mostly two of the methods are used- one method is used to raise the event.

    Now a argument is also passed as name of the event let’s say message logged.

    Emitter.emit (‘messagelogged’);

    In future you can extend logger module and every time you log a message you are going to raise an event called message logged.


    Now if you want to run this application nothing is going to happen let me show you. Because you have raised an event here but nowhere in our application you have registered a listener that is interested in that event. A Listener is a function that will be called when that event is raised.

    So, let’s register a listener that will be called when the message log event is raised. So, register a listener as-:

    So instead of writing “addlistener” ‘On’ alias is used as-:

    Emitter.on(‘’), Now it takes two arguments, first argument is name of the event, in this case i.e. “message logged” and second is callback function or the actual listener so in this a function is also passed and this function will be called when that event is raised as-:

    Emitter.on(‘messageLogged’, function(){



    So, here now message to be logged in console.


    Console.log (‘Listener called’);


    Now after run this application we have output as-:


    Copyright 1999- Ducat Creative, All rights reserved.