Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Events and emitters

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 309
    Comment on it

    Node.js comes with lots of core modules one of them is events module. We use EventEmitter in node js to create custom events and to react those custom events.
    It is something similiar as jQuery events:-

    element.on('click', function(msg) {
        console.log("The message is: " + msg);
    });

     

    Similiary, we have EventEmitter in node js for custom event.

    var events = require('events');
    var myEmitter = new events.EventEmitter();

     

    So, EventEmitter is a constructor function of class EventEmitter.

    For ex:

    var events = require('events');
    var myEmitter = new events.EventEmitter();
    
    myEmitter.on("helloEvent", function(user) {
        console.log(user + " says hello!");
    });
    // Here I am emitting the helloEvent event and passing the username along with it
    myEmitter.emit('helloEvent', "Dinesh");
    
    /*
    * Result:
    * Dinesh says hello!
    */


    Lets see the power of event emitter:

     

    var util = require('util');

    The util module allows you to do various things, one of them is to inherit certain things from object building to node.js.

     

    First of all, I am going to create a sample Person constructor.

    var Person = function(name) {
        this.name = name;
    }

     

    So, I want to inherit eventemitter to Person. For it I use the inherits method of util class.

    Syntax:

    util.inherits(constructor, superCondtructor);
    
    

    In my example:

     

    util.inherits(Person, events.EventEmitter);

     

    So, I create three person object like as follows:

    var dinesh = new Person("Dinesh");
    var ram = new Person("Ram");
    var seema = new Person("Seema");
    
    var person = [dinesh, ram, seema];
    
    person.forEach(function(personName) {
        personName.on("sayHello", function(msg) {
            console.log(personName.name + " says " + msg);
        });
    });
    
    dinesh.emit("sayHello", "Hey dude!");
    ram.emit("sayHello", "Hello world!");
    
    /*
    * Result:
    * Dinesh says Hey Dude!
    * Dinesh says Hey Dude!
    */

     

    Code goes as follows:-

    var events = require('events');
    var util = require('util');
    
    var Person = function(name) {
        this.name = name;
    }
    
    util.inherits(Person, events.EventEmitter);
    
    var dinesh = new Person("Dinesh");
    var ram = new Person("Ram");
    var seema = new Person("Seema");
    
    var person = [dinesh, ram, seema];
    
    person.forEach(function(personName) {
        personName.on("sayHello", function(msg) {
            console.log(personName.name + " says " + msg);
        });
    });
    
    dinesh.emit("sayHello", "Hey dude!");
    ram.emit("sayHello", "Hello world!");
    
    /*
    * Result:
    * Dinesh says Hey Dude!
    * Dinesh says Hey Dude!
    */

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: