Sunday, August 18, 2013

How to create your own EventEmitter in NodeJS in 3 Steps

Note: This post does not seek to explain what the EventEmitter is.  For that, look at Node's site.  I'm going to show you how to make your custom object into one.  This is handy for many reasons.  I recently created a node module which wraps grunt and exposes the output from it as an EventEmitter.


Step 1: Bring in the modules


// events contains the EventEmitter 
// class we will inherit from
var events = require('events');
// and util contains `inherit` which 
// we will use to set up our object's inheritance
var util = require('util');

Step 2: Construct your class, followed by the EventEmitter's

You will want to call the EventEmitter's constructor at the end of your own.  It's standard practice when inheriting from another class that you call into the parent's constructor in order to preserve any functionality within it.  I've excluded it before, and it's not the end of the world, it just sets some default properties.  You can see what the constructor in Node does on Github.

var MyClass = function () {
    // Your class' constructor magic

    // Call the EventEmitter constructor
    events.EventEmitter.call(this);
}

Step 3: Set up the prototypal inheritance

util.inherits(MyClass, events.EventEmitter);

Well that was easy.  What this does is set up the prototype chain for us.  You can read more about the util.inherits again on Node's website.

The Final Code

var util = require("util");
var events = require("events");

function MyClass() {
    // Your class' constructor magic

    events.EventEmitter.call(this);
}

util.inherits(MyClass, events.EventEmitter);

MyClass.prototype.emitTest = function(data) {
    this.emit("data", data);
}

// Consumption

var instance = new MyClass();
instance.on('data', function (msg) {
    console.log(msg);
});

instance.emitTest("Hello World"); //=> Hello World

9 comments:

sai said...

It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
python training institute in marathahalli
python training institute in btm
Python training course in Chennai

haripriya said...

Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.
Data Science course in Chennai
Data science course in bangalore
Data science course in pune
Data science online course

rohini said...

This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
Best Devops Training in pune
Microsoft azure training in Bangalore
Power bi training in Chennai

nisha said...

Awesome Blog. The Content is Really neat and the Concept was explained Very Clear.Keep Updating this Article.

Data Science Training Course In Chennai | Data Science Training Course In Anna Nagar | Data Science Training Course In OMR | Data Science Training Course In Porur | Data Science Training Course In Tambaram | Data Science Training Course In Velachery

lavanya said...

Wow it is really super and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.
Java training in Chennai

Java training in Bangalore

Java training in Hyderabad

Java Training in Coimbatore

Java Online Training

devi said...

Nice . I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article.thank you for sharing such a great blog with us. expecting for your.

Data Science Training In Chennai

Data Science Online Training In Chennai

Data Science Training In Bangalore

Data Science Training In Hyderabad

Data Science Training In Coimbatore

Data Science Training

Data Science Online Training

lakshmik7410 said...

Thanks for posting a very useful blog. Keep us posted!
data science training in chennai

ccna training in chennai

iot training in chennai

cyber security training in chennai

ethical hacking training in chennai

360DigiTMG said...

This post is very simple to read and appreciate without leaving any details out. Great work!
data science training in hyderabad

Unknown said...

This is a fabulous post I seen because of offer it. It is really what I expected to see trust in future you will continue in sharing such a mind boggling post data scientist course in surat