Understanding EventEmitter | Understanding Node.js Core Concepts FREE VERSION

Поділитися
Вставка
  • Опубліковано 17 гру 2024

КОМЕНТАРІ • 89

  • @viridianite
    @viridianite Рік тому +22

    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

  • @procodrr
    @procodrr Рік тому +3

    📖 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

  • @kmmonzurulislam3454
    @kmmonzurulislam3454 2 роки тому +8

    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...

  • @fanpeter-z3c
    @fanpeter-z3c 2 роки тому +1

    thanks, finally found some in-depth node tutorial

  • @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 Рік тому

      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.

  • @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 роки тому +15

      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!

  • @bicube7529
    @bicube7529 Рік тому

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

  • @confusedguy2273
    @confusedguy2273 Рік тому +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..)🤗

  • @YoannBuzenet
    @YoannBuzenet 2 роки тому +2

    thank you !!! Please continue !!

  • @psaintjean11
    @psaintjean11 6 місяців тому

    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.

  • @21adsyeshwanths.25
    @21adsyeshwanths.25 Рік тому +2

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

  • @hgsbrodi
    @hgsbrodi 2 роки тому +1

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

  • @mohammadnashashibi2942
    @mohammadnashashibi2942 10 місяців тому

    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!

  • @D_Ladybug
    @D_Ladybug Рік тому

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

  • @manasupadhyay266
    @manasupadhyay266 2 роки тому +2

    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

    • @unknown_manushya
      @unknown_manushya Рік тому

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

  • @ChannelUCkG12x
    @ChannelUCkG12x 2 роки тому

    excited to watch the next episodes, thank you very much

  • @ptkocz
    @ptkocz Рік тому +1

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

  • @hariprasath9985
    @hariprasath9985 2 роки тому

    Eagerly waiting for your next video

  • @unknown_manushya
    @unknown_manushya Рік тому

    Please complete this course!

  • @jj342
    @jj342 Рік тому

    Great video! Thanks! (completed!🎆 🍾)

  • @vishalbhalothia
    @vishalbhalothia 2 роки тому

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

  • @Talel_kraiem
    @Talel_kraiem 2 роки тому

    best explanation on youtube 😋
    please , please continue ❤

  • @memduhcevik
    @memduhcevik Рік тому

    These are exactly what I need ❤

  • @hamzaraza9804
    @hamzaraza9804 Рік тому

    Amazing video man!

  • @gaminghunter800
    @gaminghunter800 Рік тому

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

  • @sergioramos4562
    @sergioramos4562 Рік тому

    Good job!!! Please keep going

  • @CalebStephensPlaysPiano
    @CalebStephensPlaysPiano 2 роки тому

    Thanks! These videos are just what I need!

  • @NoureddineRabouane
    @NoureddineRabouane 3 місяці тому

    Thank you so much Sir , I have really enjoyed watching your videos about node js

  • @tomasgilamoedo8301
    @tomasgilamoedo8301 Рік тому

    Excellent course sir!

  • @exe.m1dn1ght
    @exe.m1dn1ght Рік тому

    Thank you so much Master for this video

  • @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.

  • @kamilp1567
    @kamilp1567 Рік тому

    Great video, learned a lot. Keep it up!

  • @zxxcfgh1629
    @zxxcfgh1629 2 роки тому

    Great
    . keep going👏💫

  • @PiroCoder
    @PiroCoder 2 роки тому

    we are missing you 😍

  • @probablytomi
    @probablytomi 2 роки тому

    great explanations! thanks

  • @Manish-hv9qx
    @Manish-hv9qx Рік тому

    brilliant job man :)

  • @negasonicteenagewarhead
    @negasonicteenagewarhead Рік тому +1

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

  • @andrepadez
    @andrepadez Рік тому +4

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

    • @solomonglenda6811
      @solomonglenda6811 Рік тому +1

      Wondering too

    • @KaleesM-tm4lw
      @KaleesM-tm4lw 3 місяці тому

      Better creating a custom event emitter from it to have a standardized code inside our codebase. That's why he started to create a separate class by inheriting the base class of event emitter 🎉

  • @dixztube
    @dixztube Рік тому

    Great job

  • @dylanm7638
    @dylanm7638 Рік тому

    great, *absolutely* great

  • @DissectTheDocs
    @DissectTheDocs Рік тому

    Great Stuff

  • @manasupadhyay266
    @manasupadhyay266 2 роки тому +1

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

  • @hakanaki
    @hakanaki Рік тому

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

  • @kobudnik
    @kobudnik Рік тому

    Stellar video

  • @casual_chess
    @casual_chess Рік тому

    you'r awesome❤

  • @thiagowwz
    @thiagowwz 7 місяців тому

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

  • @jamjam3448
    @jamjam3448 Рік тому

    woww. Thanks soo much

  • @AlanSchooll
    @AlanSchooll Рік тому

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

  • @sergioceballos3046
    @sergioceballos3046 Рік тому

    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!

  • @imranalperen
    @imranalperen 11 місяців тому

    amazing. thanks for it

  • @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 роки тому +1

      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 2 роки тому +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.

  • @mikejames6452
    @mikejames6452 2 роки тому

    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

  • @halalmeatshophk
    @halalmeatshophk Рік тому +1

    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);
      });

  • @ravikishan4116
    @ravikishan4116 Рік тому

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

  • @gabrielmachado5391
    @gabrielmachado5391 Рік тому

    Thanks!

  • @unknown_manushya
    @unknown_manushya Рік тому

    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.

  • @riyadondiraitreda2819
    @riyadondiraitreda2819 10 місяців тому

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

  • @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 Рік тому

      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.

  • @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
    }

  • @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.

  • @muhammedcansoy6131
    @muhammedcansoy6131 Рік тому

    awesome

  • @deepdive4181
    @deepdive4181 2 роки тому

    do a a video on streams

  • @giorgikhachidze64
    @giorgikhachidze64 7 місяців тому

    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?

  • @canmertinyo
    @canmertinyo Рік тому

    thanks

  • @danko5678
    @danko5678 Рік тому

    I suggest renaming master object to main object

    • @Cododev
      @Cododev  Рік тому

      That is a great point! Thanks for mentioning it!

  • @unknown_manushya
    @unknown_manushya Рік тому

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

  • @Netpilation
    @Netpilation 3 місяці тому

    do you full time or you are a freelancer , just asking for a freind

  • @aezazali710
    @aezazali710 Рік тому

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

  • @valikonen
    @valikonen 2 роки тому

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

    • @Cododev
      @Cododev  2 роки тому +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 Рік тому

    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