Coding Fun Node Js Node JS Tutorial for Beginners #8 – The Node Event Emitter

Node JS Tutorial for Beginners #8 – The Node Event Emitter

Yo gang, in this Node JS tutorial, I’ll be introducing you to the event emitter in the Node core. The event emitter allows us to wire up custom events to our own objects and then emit those events at a later point in time.

+ Repo –
+ Atom editor –

You can find more front-end development tutorials on CSS, HTML,…

Tags: , , , , , , , , , , , , , , , , , , , , , , ,

43 thoughts on “Node JS Tutorial for Beginners #8 – The Node Event Emitter”

  1. Niral Shakya says:

    Thank you for wonderful tutoreal

  2. Tom Howard says:

    Great video wonderful t-shirts and coffee mugs. Okay I said It now can I watch some relevant videos.

  3. Suraj Patil says:

    Very nice tutorial to startup with,
    in this 8th tutorial i found it was not working with me, so i tried for a workaround and it is like

    class Person extends EventEmitter{

    i am using v10.15.1, rest of the things worked fine.

  4. Hi All,

    I am getting below error while executing this example. Can you please help to understand and fix the issue.


    Object.setPrototypeOf(ctor.prototype, superCtor.prototype);


    TypeError: Object.setPrototypeOf called on null or undefined

    at Function.setPrototypeOf (<anonymous>)

    at Object.inherits (util.js:287:10)

    at Object.<anonymous> (C:MyProjectsMyNodeJSProjectsevent-emitterapp.js:16:6)

    at Module._compile (internal/modules/cjs/loader.js:689:30)

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)

    at Module.load (internal/modules/cjs/loader.js:599:32)

    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)

    at Function.Module._load (internal/modules/cjs/loader.js:530:3)

    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)

    at startup (internal/bootstrap/node.js:283:19)

  5. Damn real unit tester. I have never used util and events before

  6. youdjparents says:

    I like the typing sound your keyboard makes, what type of keyboard is it?

  7. codeasaurus says:

    There seems to be quite a few comments which reflect some confusion over the use of events. The best thing to do is to look at the observer pattern where multiple listeners exist for the same event. This should make it more clear. The example could be written like this to illustrate that.

    var events = require('events');
    var Person = function(pname) {
    var name = pname;

    var james = new Person('james');
    var mary = new Person('mary');
    var loudmouth = new Person('blabberer');

    loudmouth.mouth = new events.EventEmitter();

    //jame's observer.
    james.read_lips = function(msg){
    console.log("james found out: " + msg);

    //james adds his event to the emitter's event listener.
    james.enter_elevator = function(){
    console.log('james is in the elevator');
    loudmouth.mouth.on('elevator gossip', james.read_lips)

    //james removes his event from the emitter when he leaves the elevator.
    james.leave_elevator = function(){
    // read lips is how james responds to the event.
    loudmouth.mouth.removeListener('elevator gossip', james.read_lips);
    console.log('james has left the elevator');

    //mary's observer
    mary.overhear = function(msg){
    console.log("mary heard: " + msg);

    //mary adds her observer event to the emitter's event listeners
    mary.enter_elevator = function(){
    // overhear is how mary responds to the event.
    console.log('mary is in the elevator');
    loudmouth.mouth.on('elevator gossip', mary.overhear);

    loudmouth.speaks = function(what_is_said){
    console.log('loudmouth: ' + what_is_said);
    this.mouth.emit('elevator gossip', what_is_said);

    loudmouth.speaks('boss is having an affair');
    loudmouth.speaks('just kidding');

    The console.log() statements will illustrate the series of events and how they are observed. I hope this may help some of you who are trying to figure out the utility of events.

  8. I actually just skipped this video, this doesn't speak beginners at all

  9. Sai Nath says:

    How are random events like Key press and Mouse click handled using this Event Emitter style?

  10. Midhun Mohan says:

    This is little bit complicated

  11. Can you plz suggest or list out extensions good for node development?

  12. John N says:

    if the events var returns the event emitter then i dont understand how calling events.eventemitter() works..

  13. // better approach ES6 class
    class Person extends events.EventEmitter {
    constructor(name) {
    super(); = name;

    this.on('speak', (msg) => {
    console.log(`${} is speaking ${msg}`)

    const kriss = new Person('Kriss');
    kriss.emit('speak', 'hi');

  14. So…. Honestly, I didn't really understand the coolness of emitters. Can somebody explain me?
    It seems to be awesome thing, but I don't catch advantages of it

  15. Gester Lloyd says:

    here is a better way in my point of view :

    var events = require('events');

    var util = require('util');

    function person(name) {

    this.on('speak', function (msg) {

    console.log("n`"+name+"`"+" says: << "+msg+">>"+"n");

    }); function(msg) {

    this.emit("speak", msg);



    util.inherits(person, events.EventEmitter);

    var lmo7 = new person("lmo7");

    var bankour = new person("bankour");"it works baby !");"i'm in two !");

  16. why can't you just use james.say() vs james.emit()?

  17. m ottesen says:

    Are you able to mix patterns in one module?

  18. Jai Ko says:

    I think it's a bit unclear from this video why I can't just do:
    var Person = function(name){ = name;
    this.speak = function(msg){
    console.log(`${name} said: ${msg});

    In other words, what is special about events here that can't be just done with object method? Can you perhaps give more practical example used in real world?

  19. Keren Duchan says:

    Shaun, I think this great tutorial series needs to be updated in accordance with the newer syntax of ES6 as other commenters have pointed out.

  20. Is this really for beginners ??? damn confusing.

  21. Amar says:

    if you are using a new version of Node.js
    you dont have to require util , just use a class and extend it like the following

    const events = require('events');

    class Person extends events.EventEmitter{
    super(); = name;

    let james = new Person('james');
    let mary = new Person('mary');
    let ruy = new Person('ruy');
    let people = [james, mary, ruy];

    people.forEach(person => {
    person.on('speak', msg => {
    console.log( + ' said : ' + msg);

    james.emit('speak', 'hey dudes');

  22. I got
    'TypeError: The super constructor to "inherits" must not be null or undefined'
    This helped me (instead of events.eventEmitter):
    util.inherits(Person, events);

  23. Koivinen says:

    I see there is obviously some "if this, then that" logic to it, but what is the takeaway of this? To me as a noob, this just demonstrates a complicated alternative of console.log(), to put it lightly. Shouldn't this be more like a Node JS Tutorial for Beginners #137 ?

  24. Bryan Dike says:

    Hello net Ninja. I am a bit confused with how you are able to make james.emit as a function call when it is not on the global object.

  25. Tan Zi GanG says:

    this better than crappy tutorial in pluralsight

  26. Ramim Uddin says:

    I have a problem, the console says that the person.on is not a function, what do I do ?

  27. Quick question… why create an event on object if you can call straight a function of this object.
    I mean instead of writing

    james.emit('speak', 'hey dudes');

    why do not straight call a function speak:
    I mean create a function speak and write:

    james.speak('hey dudes');

    The result would be the same.
    I don't see the point to do something like that.

    Otherwise you would need to create 2 differents object (Person and Alien), add the same event
    Add Person and Alien in the same array and then call the same event in a loop for these 2 objects.
    This way I could see the point.

    In this case, why do not learn properly prototype in js and apply it.
    It will do the same thing.
    #BrainStorming lol

    Also, thanks a lot for your tutorials, they explain very well, it is very clear! 🙂

  28. Hi! I'm at 4:57 and I don't understand why the 2nd argument in emit method is directly passed as mssg. I also took a look at Node.js documentation but it's still not clear to me.

  29. Errors out saying '
    TypeError: The super constructor to "inherits" must not be null or undefined'

  30. ivan89pln says:

    I wish i could find you in the real life so i can buy you a beer,great job on your videos!

  31. This code did not work for me

    var events = require('events');

    var util = require('util');

    var Person = function(name){ = name;


    util.inherits(Person, events.EventEmitter);

    var sami = new Person('Sami');

    var Anes = new Person('Anes');

    var Walid = new Person('Walid');

    var people = [Sami, Anes, Walid];


    person.on('Speak', function(mssg){

    console.log( + ' Said: ' + mssg);



    sami.emit('Speak', 'Hey Dudes');

    anes.emit('Speak', 'Hey Dudes');
    This is what appeared in the Terminal

    PS C:UsersAmine BoukerdouhDesktopNode-JS-Playlist-Amine> node app
    C:UsersAmine BoukerdouhDesktopNode-JS-Playlist-Amineapp.js:94
    var people = [Sami, Anes, Walid];

    ReferenceError: Sami is not defined
    at Object.<anonymous> (C:UsersAmine BoukerdouhDesktopNode-JS-Playlist-Amineapp.js:94:15)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)

    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:188:16)
    at bootstrap_node.js:609:3
    PS C:UsersAmine BoukerdouhDesktopNode-JS-Playlist-Amine>

    please hamp me

  32. Which theme are you using in atom?

  33. HI, best tutorials for Node.js. thanks a lot for sharing knowledge.

    I have a query Is it mandatory to give this name only : var events = require('events');

    I tried with : var diffevents= require('diffevents');
    but giving this error

    Error: Cannot find module 'diffevents'
    at Function.Module._resolveFilename (module.js:547:15)
    at Function.Module._load (module.js:474:25)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (E:CodingNodeJsWorkspaceHandsOnsep8a.js:3:18)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)

  34. Autif Kamal says:

    Minus 10 points…Ryu never said "hadouken!"

  35. K Galaxy says:

    What does the on() do? Is it a function in express? Is there a reference?

  36. Why you have used foreach when this can be done without using the same?

  37. I have seen all playlist still little bit confused for routing and other ejs problem can Anyone give me another tutorial or demo link?

  38. Sweet Potato says:

    It seems like Node.js creates too many extra steps for the fun of it. Thanks for the lesson.

  39. Not sure why we need a forEach loop here. Can we not do the same thing w/o it by just having person.on function defined?

  40. Please please…enlighten me on callback Functions.

  41. Brayheart says:

    seriously they're simple and short, id much rather watch a bunch of short videos than one really long one

  42. Brayheart says:

    I love this channel, thankyou so much

Leave a Reply

Your email address will not be published. Required fields are marked *