SHARE

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.

—– COURSE LINKS:
+ Repo – https://github.com/iamshaunjp/node-js-playlist
+ Atom editor – https://atom.io/

———————————————————————————————
You can find more front-end development tutorials on CSS, HTML, JavaScript, jQuery, WordPress & more on the channel homepage…

SUBSCRIBE TO CHANNEL – https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg?sub_confirmation=1

========== JavaScript for Beginners Playlist ==========

https://www.youtube.com/playlist?list=PL4cUxeGkcC9i9Ae2D9Ee1RvylH38dKuET

============ CSS for Beginners Playlist =============

https://www.youtube.com/playlist?list=PL4cUxeGkcC9gQeDH6xYhmO-db2mhoTSrT

============== The Net Ninja =====================

For more front-end development tutorials & to black-belt your coding skills, head over to – https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg or http://thenetninja.co.uk

================== Social Links ==================

Twitter – @TheNetNinja – https://twitter.com/thenetninjauk

31 COMMENTS

  1. Better than my college lessons… Thank you so much Ninja Shaun! 🙂
    Just one little question: Do I need to run a npm install for events as well? Or it should be in my node_modules already?

  2. If you're watching this tutorial now & are using ES6 you might get the error 'Object.setPrototypeOf(ctor.prototype, superCtor.prototype);'. Here's the solution, hope you can make sense out of it, ES6 does not use 'inherits', inteads uses 'extends':

    const events = require('events');
    const util = require('util');

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

    const joe = new Person('joe');
    const gladys = new Person('gladys');
    const kaya = new Person('kaya');

    const people = [joe, gladys, kaya];

    people.forEach(function(person){
    person.on('speak', function(msg){
    console.log(person.name + ' says ' + msg);
    });
    });

    joe.emit('speak', 'ES6, the heck!');

  3. The problem with this tutorial is that it's not motivating the user. WHY is the event emitter such a good thing? Many of your statements seem geared towards beginners, but they don't have the motivation nor experience to understand why certain patterns are useful, and what for.

  4. Excellent series so far, you have a way of making these things accessible to us simple folk. I didnt realize how much I needed Node JS until I watched these vids.

  5. Here is my way of doing it, assuming I have some sort of data coming from somewhere and I have stored that in an object called "DATA";
    You can give this a go and see for yourself;

    var events = require('events');
    var util = require('util');
    // Constructor
    var Person = function (detail) {
    this.name = detail.name,
    this.dob = detail.dob;
    };
    util.inherits(Person, events.EventEmitter);
    var DATA = [
    { name: 'Mary', dob: '07/11/1980' },
    { name: 'Farzad', dob: '08/09/1987' },
    { name: 'James', dob: '02/03/1992' },
    { name: 'Darius', dob: '04/06/1973' },
    { name: 'Andy', dob: '01/04/1954' },
    { name: 'Ryu', dob: '27/12/1968' }
    ];
    var people = [];
    DATA.forEach(person => {
    person = new Person({ name: person.name, dob: person.dob });
    person.on('speak', msg => {
    console.log(`${person.name} said: ${msg}`);
    });
    people.push(person);
    });
    people[0].emit('speak', 'Hi, how are you doing today?');

    And of I course I wanted to thank TheNetNinja for his great series of tutorials 🙂
    Give it a like
    Many thanks

  6. Please look at this script, It should give 1 2 3 4 as output but gives 1 2 3 4 5, Why is it so…
    #NodeJs
    #EventEmitter
    #On()
    #Once()

    var events= require('events')

    var emitter= new events.EventEmitter();

    let m=0;
    //to handle an event only once and ignore the after calls
    // .once() is used.

    emitter.once('singleton event', function() {
    console.log(++m);

    })

    console.log('call 1 at once');
    emitter.emit('singleton event');

    //Now the eventListener is unregistered

    console.log('call 2 at once');
    console.log('ignored');
    emitter.emit('singleton event');

    emitter.on('singleton event', function() {
    console.log(++m);
    })
    console.log('call 3 at on');
    emitter.emit('singleton event');

    emitter.once('singleton event', function() {
    console.log(++m);

    })
    console.log('4th call at once');
    emitter.emit('singleton event');

    console.log('5th call at once');

    emitter.emit('singleton event');

  7. By far the best explaining of node. What is this and why it's there – approach. You don't have to go in dept, there are some more complex tutorials for that.

  8. Hey, so I was just wondering if you knew how to increase the Maximum Eventemitter Listeners. I have 12 commands on my discord bot, but only the first 11 work because the max listeners is set to 11. What is the fix for this? Thanks!

  9. You are emitting events after the forEach loop. So initially when the forEach loop runs, there is nothing to emit. But how does it work when you emit events at the end?

  10. If you're watching this, using Node 7.8.*, you might get a TypeError saying events.EventEmitter is not a constructor.

    To fix this do var myEmitter = new events; 😉

  11. Hi, I want to ask that, what is the general purpose to use Event Emitter? Anyone here have a real world example why we need to use it ?

  12. I just came back to this to get started again and it seems that (with webstorm) with each new startup, you'll need to enable Node again (File > Settings > Languages & Frameworks > Node.js and NPM > click "Enable" button). Otherwise, some of your stuff will come up "undefined" and you'll get errors.

LEAVE A REPLY

Please enter your comment!
Please enter your name here