Understanding EventEmitter | Understanding Node.js Core Concepts FREE VERSION

Sdílet
Vložit
  • čas přidán 20. 05. 2022
  • Grab the complete and premium version of the course using this link:
    www.udemy.com/course/understa...
    In this video, we'll understand what exactly EventEmitter, which is an important object in Node.js, really is.
    The Course - Free Version: • Understanding Node.js ...
    Code your own event emitter in Node.js: www.freecodecamp.org/news/how...
    Learn those JavaScript concepts from this page: www.pagser.com/joseph/js
    UPDATE (June 2022): As Arthur mentioned in the comments, there's still an issue in the events.js source code, and that quick fix will now regard all onceWrapper functions as the same thing. This will be problematic if we declare multiple once listeners with the same event name. If you take a look at the Node.js events.js file, you'll see how Node.js is handling those "once" functions.
    I'll post an update if I get the chance to fix this issue. I'll also appreciate it if you can share your solutions to the problem if you come up with one!
    --------------------------------------------------------------------
    My LinkedIn Profile: / joseph-heidari
    Cododev Website: www.cododev.ca/

Komentáře • 87

  • @viridianite
    @viridianite Před rokem +21

    00:00 Introduction
    3:46 What's the EventEmitter object?
    5:14 Example of events in the OS
    11:12 Diagram: What happens when 'on' and 'emit' methods run
    14:05 Code: Running the example
    17:50 Diagram: The 'once' method in the EventEmitter class
    19:42 Reading the NodeJS docs about the EventEmitter class
    23:40 Looking over an implementation of the EventEmitter class
    31:42 Looking over the source code of the EventEmitter class in NodeJS
    34:38 Final words

  • @hgsbrodi
    @hgsbrodi Před 2 lety +1

    Nice, I was watching videos on design patterns and observers the other day, and this was a great refresher.

  • @ptkocz
    @ptkocz Před rokem +1

    Amazing content 🔥, please keep going with stuff like this! :)

  • @kmmonzurulislam3454
    @kmmonzurulislam3454 Před rokem +7

    This is how you explain a complicated topic. Thanks for making such a helpful video and helping us. You got one more subscriber! Please keep going...

  • @confusedguy2273
    @confusedguy2273 Před 11 měsíci +2

    Thank you for your videos....they really help to get a deep insight into how Node works..There are very few creators who make such high quality videos on core concepts ..Thank you..Hope to see your more videos on various topics in future...(P.S your video Nodejs Under the hood really helped me at a recent interview...Thanks to you I cleared it..)🤗

  • @ChannelUCkG12x
    @ChannelUCkG12x Před 2 lety

    excited to watch the next episodes, thank you very much

  • @user-lw6jt1nt4k
    @user-lw6jt1nt4k Před rokem +1

    thanks, finally found some in-depth node tutorial

  • @YoannBuzenet
    @YoannBuzenet Před 2 lety +2

    thank you !!! Please continue !!

  • @psaintjean11
    @psaintjean11 Před měsícem

    Excellent Lesson!! I am new to coding, new to JavaScript and I could understand better the concepts of the programming language. Thanks for explaining thoroughly event emitters.

  • @CalebStephensPlaysPiano

    Thanks! These videos are just what I need!

  • @kamilp1567
    @kamilp1567 Před rokem

    Great video, learned a lot. Keep it up!

  • @procodrr
    @procodrr Před 10 měsíci +2

    📖 Chapters
    00:00 - Understanding EventEmitter
    02:20 - On and Emit Methods on Emitter Object
    04:55 - Understanding the Event-Driven Nature of OS
    08:15 - Understanding the Event-Driven Nature of Node.js
    10:26 - How EventEmitter Works Behind the Scenes?
    17:35 - Once Method on Emitter Object
    19:27 - Learn More About EventEmitter from Node.js Docs
    23:16 - Creating Our Own Version of EventEmitter
    31:38 - Taking a Look at the Source Code of Node.js for EventEmitter
    34:36 - Wrapping Up

  • @Talel_kraiem
    @Talel_kraiem Před rokem

    best explanation on youtube 😋
    please , please continue ❤

  • @tomasgilamoedo8301
    @tomasgilamoedo8301 Před rokem

    Excellent course sir!

  • @gaminghunter800
    @gaminghunter800 Před rokem

    wow, amazing....thanks for this knowledge
    🤩

  • @probablytomi
    @probablytomi Před rokem

    great explanations! thanks

  • @_ademmeral
    @_ademmeral Před 11 měsíci

    These are exactly what I need ❤

  • @vishalbhalothia
    @vishalbhalothia Před 2 lety

    Great videos 👍👍👍👍👍
    Need more and more videos.... complete node js list...

  • @hariprasath9985
    @hariprasath9985 Před 2 lety

    Eagerly waiting for your next video

  • @mohammadnashashibi2942
    @mohammadnashashibi2942 Před 5 měsíci

    in the "once" implementation if you had just changed "i > 0" ---> "i >= 0" in the for loop it would've fixed it. You can compare functions in JS by ref which is the wrapperFn in this case. great quality content btw!👌thanks!

  • @hamzaraza9804
    @hamzaraza9804 Před 8 měsíci

    Amazing video man!

  • @sergioramos4562
    @sergioramos4562 Před rokem

    Good job!!! Please keep going

  • @exe.m1dn1ght
    @exe.m1dn1ght Před 8 měsíci

    Thank you so much Master for this video

  • @arthurfedotiew3609
    @arthurfedotiew3609 Před 2 lety +6

    The one thing I personally didn't like is INTENTIONAL skipping of proper computer science terminology. Imho, it's only beneficial to us, students, to hear as much as possible proper terminology used for explanation from the get go. That is very confusing in a web to understand all this stuff like concurrency, parallelism, push-pull, etc. This is due to the fact that there is lot of frauds, and you are NOT one of them. You have a deep knowledge and great skills of teaching. Please use all of your knowledge, since it's a precise for all of us!
    Apart from that, everything is at highest level of professionalism! Keep it up!)

    • @Cododev
      @Cododev  Před 2 lety +13

      Hi Arthur, sorry for my late response...
      I think it's necessary to mention these computer science terms, due to two main reasons. One is that some students already possess some knowledge in CS, maybe they have studied CS in university, or somewhat somehow have learned these concepts through their experience, and then mentioning them here will make the concept easier for them to grasp and make better connections in their minds. And another is that some people are just curious to know and would research some of these terms and learn about them, which will ultimately result in a deeper knowledge of the technology.
      The downside is that some students neither have a CS background nor care to learn about them and these terms may only confuse them if used too much.
      But I should take better care here, focus on a more complete and accurate use of these terms, or maybe give a pdf file with each video that incorporates all these terms with some description and probably links to learn more... I hope I could make some improvements in this regard! and thanks for your comment Arthur!

  • @jj342
    @jj342 Před rokem

    Great video! Thanks! (completed!🎆 🍾)

  • @zxxcfgh1629
    @zxxcfgh1629 Před 2 lety

    Great
    . keep going👏💫

  • @Manish-hv9qx
    @Manish-hv9qx Před 11 měsíci

    brilliant job man :)

  • @abhimanyu2350
    @abhimanyu2350 Před rokem +1

    The once implementation is actually correct, it's only the for loop statement that's buggy @29:46. We can compare the two functions in this case cause we are actually comparing the references to same memory location

    • @VA-nk6jc
      @VA-nk6jc Před 10 měsíci

      I noticed that too. The loop simply doesn't reach 0 index.
      And it's perfectly fine to compare functions (objects) in JS. How the author could not know that? It's quite a serious mistake.

  • @dylanm7638
    @dylanm7638 Před rokem

    great, *absolutely* great

  • @dixztube
    @dixztube Před rokem

    Great job

  • @kobudnik
    @kobudnik Před rokem

    Stellar video

  • @syedsadiq8591
    @syedsadiq8591 Před 11 měsíci

    Great Stuff

  • @jamjam3448
    @jamjam3448 Před rokem

    woww. Thanks soo much

  • @bicube7529
    @bicube7529 Před rokem

    ok, i'm feeling that I didn't know javascript before. i truly understand now how the event emitter works

  • @imranalperenbayram7847
    @imranalperenbayram7847 Před 7 měsíci

    amazing. thanks for it

  • @suyashpurwar631
    @suyashpurwar631 Před rokem

    Please complete this course!

  • @gabrielmachado5391
    @gabrielmachado5391 Před rokem

    Thanks!

  • @D_Ladybug
    @D_Ladybug Před 9 měsíci

    Juat stumbling on this playlist and I'm addicted already.
    Could you please teach us C too?

  • @casual_chess
    @casual_chess Před 11 měsíci

    you'r awesome❤

  • @21adsyeshwanths.25
    @21adsyeshwanths.25 Před rokem +1

    I would like u to contribute more bro, not sure about others but i have been gaining a lot from you................
    Support from India❤

  • @mykun8737
    @mykun8737 Před rokem

    Dear Cododev, could you please add a timestamp to the video's table of contents and include the source code for the video, placing it in the description section? I'm thrilled that you are the only one teaching about Node.js core. In the outside world, people seem to be solely focused on teaching frameworks like Express.js.

  • @thiagowwz
    @thiagowwz Před 2 měsíci

    *_a m a z i n g_* lesson, whoa!

  • @PiroCoder
    @PiroCoder Před rokem

    we are missing you 😍

  • @kareemmohamed9591
    @kareemmohamed9591 Před rokem

    please make a course about streams as your explanation is very useful and there is no good resources about streams out there

  • @negasonicteenagewarhead
    @negasonicteenagewarhead Před rokem +1

    Great teaching. can someone tell why there's no content in reference site.

  • @muhammedcansoy6131
    @muhammedcansoy6131 Před rokem

    awesome

  • @hakanaki
    @hakanaki Před 7 měsíci

    So does it make sense to replaces services with just events? for example when a user signs up, you emit the the sendEmail event.

  • @sergioceballos3046
    @sergioceballos3046 Před rokem

    hey bro amazing tutorials, but what about clusters and workers? It would be great if you can explain it to us. Thanks a lot for your videos. Keep it up man!

  • @manasupadhyay266
    @manasupadhyay266 Před 2 lety +1

    Sir, please upload videos , your videos are very different from other node js videos available in CZcams or any paid course

  • @canmertinyo
    @canmertinyo Před 9 měsíci

    thanks

  • @farhanbajwa4954
    @farhanbajwa4954 Před rokem

    Can you make more videos on node js , express js, and react js performance.

  • @andrepadez
    @andrepadez Před rokem +4

    Great video. Why is it necessary to extend the EventEmitter class and not instanciate from it directly?

  • @mikejames6452
    @mikejames6452 Před rokem

    Hi one question if any can help .
    If we pass arguments in event emiiter s once call back function it gets undefined even if we passed it in emit function for that event

  • @manasupadhyay266
    @manasupadhyay266 Před 2 lety +1

    Excited to learn how http module works😁

    • @Cododev
      @Cododev  Před 2 lety +2

      It's coming up next after File System, Streams, Net and then HTTP!

    • @manasupadhyay266
      @manasupadhyay266 Před 2 lety +2

      Waiting for your next video 😅

    • @YoannBuzenet
      @YoannBuzenet Před 2 lety +1

      @@manasupadhyay266 +1 :D

    • @suyashpurwar631
      @suyashpurwar631 Před rokem

      @@Cododev Please bring it on. Include HTTP/2 and HTTPS.

  • @ravikishan4116
    @ravikishan4116 Před 7 měsíci

    Why you remove the other videos I want that content please sir

  • @riyadondiraitreda2819
    @riyadondiraitreda2819 Před 5 měsíci

    Hi man I have some questions. Can. I ask it plais

  • @deepdive4181
    @deepdive4181 Před rokem

    do a a video on streams

  • @gouthamkrishna5490
    @gouthamkrishna5490 Před rokem

    DO I need to know about core concepts before creating own projects.I mean Im stuck in a particular level I cant go beyond that.

  • @arthurfedotiew3609
    @arthurfedotiew3609 Před 2 lety +1

    Wouldn't your quick fix of removeEventListener remove all "once's" listeners for particular event, taking onto account that wrapper function - onceWrapper.toString( ) is always the same string?

    • @Cododev
      @Cododev  Před 2 lety

      Yeah you're right Arthur, I should've paid more attention to that fix haha. The issue is exactly what you've mentioned, it regards all the onceWrapper functions as the same thing.
      We'll face the issue if we declare multiple once listeners with a unique event name. For example, 3 once listeners on an object listening to the event 'bar', and each one logs the number like this: '...occurred 1.', '... occurred 2', '... occurred 3'.
      Now, if we run myE.emit("bar") a hundred times, for example, we should get the logs in this order: '... occurred 1.', '... occurred 2', '... occurred 3'.
      But, in our code we get: '... occurred 1.', '... occurred 3', '... occurred 3'.
      But anyway, thanks for mentioning this. I'll try to come up with something better (the node.js solution is also pretty interesting, with 2 wrapper functions, a fired state...), in the meantime I'll update the video description to mention this issue, thanks again!

    • @squarerootof2
      @squarerootof2 Před rokem +1

      @@Cododev I think the code from freecodecamp is designed to be run with named functions because removeEventListener is trying to compare two references and if you push anonymous functions onto the array when adding listeners there are no references to compare.
      No sure though since I've only just started learning this quirky, weird language and I haven't really tried the code yet, lol.
      Great channel.

  • @giorgikhachidze64
    @giorgikhachidze64 Před 3 měsíci

    Why did you create a new class and inherited? What was the point of that exactly? Why didn't you just use the class you already imported?

  • @suyashpurwar631
    @suyashpurwar631 Před rokem

    Created an infinite loop using two events. Throws error when maximum call stack size is exceeded.
    const EventEmitter = require('events');
    const ee = new EventEmitter();
    ee.on('eventOne', function() {
    console.log(this);
    this.emit('eventTwo');
    });
    ee.on('eventTwo', function() {
    console.log('eventTwo called');
    this.emit('eventOne');
    });
    ee.emit('eventOne');

  • @kartikparmar3496
    @kartikparmar3496 Před rokem

    Your pagser page is empty, there is no list of any articles or anything.

  • @halalmeatshophk
    @halalmeatshophk Před rokem

    when does an "error" event occur ?
    i assume that .emit('error', ) would only occur if an error is found, but my code is always returning an error.
    I wonder what is the use of this if it always return an error, instead of only when error occurs.

    • @viridianite
      @viridianite Před rokem +1

      Whenever the code that extends the EventEmitter class throws an error and emits as 'error'

    • @Cododev
      @Cododev  Před rokem +2

      The error event will occur if we do .emit('error', ). It doesn't have to be automatic, you could manually do this. So imagine you have a classroom object extending the EventEmiter that has an event called 'newStudent'. Now in your code you could have a limit for number of students, let's say 30. If we try to add another student once we reach the limit, then we could emit an error indicating that.
      Hopefully this code will clarify that for you:
      classroom.on("newStudent", () => {
      if (students.length < 30) {
      // adding a new student to our database...
      } else {
      classroom.emit('error', new Error("Cannot add more than 30 students."));
      }
      });
      // A place to handle all the errors relating to the classroom
      classroom.on("error", (err) => {
      // sending the error that was occurred to client
      res.status(400).send(err.message);
      });

  • @NitinMandale-sm6th
    @NitinMandale-sm6th Před rokem

    why are we creating Emitter class which extents from EventEmitter and dont have anything inside it... cant we directly create a instance of EventEmitter ?? please clarify

    • @viridianite
      @viridianite Před rokem

      Think of the EventEmitter class an interface that classes that extend it must abide by. There's not much point on simply extending the EventEmitter class and not implementing the necessary emitters.

    • @miggu
      @miggu Před 10 měsíci

      Not necessary for this particular example, but useful if you want to create a class that contains its own methods, those methods in this case would typically be listeners.

  • @suyashpurwar631
    @suyashpurwar631 Před rokem

    So, created a never ending loop
    ee.on('eventThree', function() {
    console.log('called');
    this.emit('eventThree');
    });
    ee.emit('eventThree');

  • @danko5678
    @danko5678 Před rokem

    I suggest renaming master object to main object

    • @Cododev
      @Cododev  Před rokem

      That is a great point! Thanks for mentioning it!

  • @mjprakash1780
    @mjprakash1780 Před rokem

    30:31 i think off method should be something like this correct me if i am worng
    off(eventName) {
    delete this.listeners[eventName]
    return this
    }

  • @aezazali710
    @aezazali710 Před rokem

    it was not same. once was getting called five times by event.emit('bar')

  • @valikonen
    @valikonen Před rokem

    I'm JS/TS dev but I'm in doubts node has a future. go and rust it will take all....

    • @Cododev
      @Cododev  Před rokem +8

      Hi Carnaru, thanks for your comment!
      Well in response I have to say that is certainly not true. While it is possible in the tech world for a new thing to come out and take over another, Node.js will stay here for years to come. And if it’s going to be overtaken by something else, that’s not going to be Go or Rust.
      Rust and Go are great technologies, I love working with them, but it’s important to know that Node, Rust, Go, Java, and Python… are just tools and each tool is good for some specific tasks while another may not suit that job well.
      Now comparing these together will take at least an article and I can’t really say all that in a comment. Go is better at concurrent programming and multi-threading, but still, if you combine Node and C++ (you’ll get the best of both worlds), you’ll come up with something impossible to beat by Go or Rust.
      I’ve seen lots of articles online saying the cons of Node, but most of them are outdated and really the authors don’t have enough knowledge of Node so they just throw wrong facts at people (mainly it’s because good tutorials and resources that teach node in depth out there are so scarce).
      Also, for learning purposes, we should focus on the core concepts, topics like Databases, Operating Systems, Security, Algorithms and Data Structures, and Networking. Node is a great candidate to use to learn these things, and honestly, it doesn’t really matter what you pick to learn these, just master them with whatever you feel most comfortable with, and I promise you if later you want to move to another back end technology, it’ll be a cinch!
      Best Regards

    • @viridianite
      @viridianite Před rokem +1

      Rust will certainly take over. The laws of physics are being written in it as we speak.

  • @kiranmnrao1914
    @kiranmnrao1914 Před rokem

    I think you forgot to remove the property from the master object when we call myE.once().
    removeListener (eventName, fn) {
    let lis = this.listeners[eventName];
    if (!lis) return this;
    for(let i = 0; i