Understanding EventEmitter | Understanding Node.js Core Concepts FREE VERSION

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 พ.ค. 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/

ความคิดเห็น • 87

  • @viridianite
    @viridianite ปีที่แล้ว +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

  • @ChannelUCkG12x
    @ChannelUCkG12x 2 ปีที่แล้ว

    excited to watch the next episodes, thank you very much

  • @YoannBuzenet
    @YoannBuzenet 2 ปีที่แล้ว +2

    thank you !!! Please continue !!

  • @ptkocz
    @ptkocz ปีที่แล้ว +1

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

  • @hgsbrodi
    @hgsbrodi 2 ปีที่แล้ว +1

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

  • @CalebStephensPlaysPiano
    @CalebStephensPlaysPiano ปีที่แล้ว

    Thanks! These videos are just what I need!

  • @user-lw6jt1nt4k
    @user-lw6jt1nt4k ปีที่แล้ว +1

    thanks, finally found some in-depth node tutorial

  • @kamilp1567
    @kamilp1567 ปีที่แล้ว

    Great video, learned a lot. Keep it up!

  • @kmmonzurulislam3454
    @kmmonzurulislam3454 ปีที่แล้ว +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...

  • @_ademmeral
    @_ademmeral 11 หลายเดือนก่อน

    These are exactly what I need ❤

  • @Talel_kraiem
    @Talel_kraiem ปีที่แล้ว

    best explanation on youtube 😋
    please , please continue ❤

  • @confusedguy2273
    @confusedguy2273 11 หลายเดือนก่อน +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..)🤗

  • @tomasgilamoedo8301
    @tomasgilamoedo8301 ปีที่แล้ว

    Excellent course sir!

  • @probablytomi
    @probablytomi ปีที่แล้ว

    great explanations! thanks

  • @arthurfedotiew3609
    @arthurfedotiew3609 2 ปีที่แล้ว +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  2 ปีที่แล้ว +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!

  • @hamzaraza9804
    @hamzaraza9804 8 หลายเดือนก่อน

    Amazing video man!

  • @hariprasath9985
    @hariprasath9985 2 ปีที่แล้ว

    Eagerly waiting for your next video

  • @sergioramos4562
    @sergioramos4562 ปีที่แล้ว

    Good job!!! Please keep going

  • @procodrr
    @procodrr 10 หลายเดือนก่อน +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

  • @gaminghunter800
    @gaminghunter800 ปีที่แล้ว

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

  • @exe.m1dn1ght
    @exe.m1dn1ght 8 หลายเดือนก่อน

    Thank you so much Master for this video

  • @dylanm7638
    @dylanm7638 ปีที่แล้ว

    great, *absolutely* great

  • @Manish-hv9qx
    @Manish-hv9qx 11 หลายเดือนก่อน

    brilliant job man :)

  • @zxxcfgh1629
    @zxxcfgh1629 2 ปีที่แล้ว

    Great
    . keep going👏💫

  • @vishalbhalothia
    @vishalbhalothia 2 ปีที่แล้ว

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

  • @syedsadiq8591
    @syedsadiq8591 10 หลายเดือนก่อน

    Great Stuff

  • @jj342
    @jj342 ปีที่แล้ว

    Great video! Thanks! (completed!🎆 🍾)

  • @kobudnik
    @kobudnik ปีที่แล้ว

    Stellar video

  • @dixztube
    @dixztube ปีที่แล้ว

    Great job

  • @jamjam3448
    @jamjam3448 ปีที่แล้ว

    woww. Thanks soo much

  • @bicube7529
    @bicube7529 ปีที่แล้ว

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

  • @psaintjean11
    @psaintjean11 หลายเดือนก่อน

    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.

  • @imranalperenbayram7847
    @imranalperenbayram7847 6 หลายเดือนก่อน

    amazing. thanks for it

  • @gabrielmachado5391
    @gabrielmachado5391 ปีที่แล้ว

    Thanks!

  • @casual_chess
    @casual_chess 10 หลายเดือนก่อน

    you'r awesome❤

  • @PiroCoder
    @PiroCoder ปีที่แล้ว

    we are missing you 😍

  • @suyashpurwar631
    @suyashpurwar631 ปีที่แล้ว

    Please complete this course!

  • @thiagowwz
    @thiagowwz 2 หลายเดือนก่อน

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

  • @21adsyeshwanths.25
    @21adsyeshwanths.25 ปีที่แล้ว +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❤

  • @D_Ladybug
    @D_Ladybug 9 หลายเดือนก่อน

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

  • @muhammedcansoy6131
    @muhammedcansoy6131 ปีที่แล้ว

    awesome

  • @mohammadnashashibi2942
    @mohammadnashashibi2942 5 หลายเดือนก่อน

    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!

  • @abhimanyu2350
    @abhimanyu2350 ปีที่แล้ว +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 9 หลายเดือนก่อน

      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.

  • @mykun8737
    @mykun8737 ปีที่แล้ว

    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.

  • @canmertinyo
    @canmertinyo 8 หลายเดือนก่อน

    thanks

  • @kareemmohamed9591
    @kareemmohamed9591 ปีที่แล้ว

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

  • @sergioceballos3046
    @sergioceballos3046 11 หลายเดือนก่อน

    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 2 ปีที่แล้ว +1

    Excited to learn how http module works😁

    • @Cododev
      @Cododev  2 ปีที่แล้ว +2

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

    • @manasupadhyay266
      @manasupadhyay266 2 ปีที่แล้ว +2

      Waiting for your next video 😅

    • @YoannBuzenet
      @YoannBuzenet 2 ปีที่แล้ว +1

      @@manasupadhyay266 +1 :D

    • @suyashpurwar631
      @suyashpurwar631 ปีที่แล้ว

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

  • @manasupadhyay266
    @manasupadhyay266 2 ปีที่แล้ว +1

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

  • @negasonicteenagewarhead
    @negasonicteenagewarhead ปีที่แล้ว +1

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

  • @hakanaki
    @hakanaki 7 หลายเดือนก่อน

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

  • @farhanbajwa4954
    @farhanbajwa4954 ปีที่แล้ว

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

  • @andrepadez
    @andrepadez ปีที่แล้ว +4

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

  • @deepdive4181
    @deepdive4181 ปีที่แล้ว

    do a a video on streams

  • @mikejames6452
    @mikejames6452 ปีที่แล้ว

    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

  • @arthurfedotiew3609
    @arthurfedotiew3609 2 ปีที่แล้ว +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  2 ปีที่แล้ว

      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 ปีที่แล้ว +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.

  • @ravikishan4116
    @ravikishan4116 7 หลายเดือนก่อน

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

  • @riyadondiraitreda2819
    @riyadondiraitreda2819 5 หลายเดือนก่อน

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

  • @halalmeatshophk
    @halalmeatshophk ปีที่แล้ว

    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 ปีที่แล้ว +1

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

    • @Cododev
      @Cododev  ปีที่แล้ว +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);
      });

  • @gouthamkrishna5490
    @gouthamkrishna5490 ปีที่แล้ว

    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.

  • @suyashpurwar631
    @suyashpurwar631 ปีที่แล้ว

    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 ปีที่แล้ว

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

  • @NitinMandale-sm6th
    @NitinMandale-sm6th ปีที่แล้ว

    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 ปีที่แล้ว

      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 9 หลายเดือนก่อน

      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.

  • @giorgikhachidze64
    @giorgikhachidze64 2 หลายเดือนก่อน

    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?

  • @danko5678
    @danko5678 ปีที่แล้ว

    I suggest renaming master object to main object

    • @Cododev
      @Cododev  ปีที่แล้ว

      That is a great point! Thanks for mentioning it!

  • @suyashpurwar631
    @suyashpurwar631 ปีที่แล้ว

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

  • @mjprakash1780
    @mjprakash1780 ปีที่แล้ว

    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 ปีที่แล้ว

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

  • @valikonen
    @valikonen ปีที่แล้ว

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

    • @Cododev
      @Cododev  ปีที่แล้ว +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 ปีที่แล้ว +1

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

  • @kiranmnrao1914
    @kiranmnrao1914 11 หลายเดือนก่อน

    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