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
📖 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
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
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.
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!)
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!
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..)🤗
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.
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!
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.
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 🎉
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!
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?
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!
@@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.
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
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.
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); });
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');
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
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.
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.
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
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
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
📖 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
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...
thanks, finally found some in-depth node tutorial
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
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.
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!)
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!
ok, i'm feeling that I didn't know javascript before. i truly understand now how the event emitter works
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..)🤗
thank you !!! Please continue !!
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.
I would like u to contribute more bro, not sure about others but i have been gaining a lot from you................
Support from India❤
Nice, I was watching videos on design patterns and observers the other day, and this was a great refresher.
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!
Juat stumbling on this playlist and I'm addicted already.
Could you please teach us C too?
Excited to learn how http module works😁
It's coming up next after File System, Streams, Net and then HTTP!
Waiting for your next video 😅
@@manasupadhyay266 +1 :D
@@Cododev Please bring it on. Include HTTP/2 and HTTPS.
excited to watch the next episodes, thank you very much
Amazing content 🔥, please keep going with stuff like this! :)
Eagerly waiting for your next video
Please complete this course!
Great video! Thanks! (completed!🎆 🍾)
Great videos 👍👍👍👍👍
Need more and more videos.... complete node js list...
best explanation on youtube 😋
please , please continue ❤
These are exactly what I need ❤
Amazing video man!
wow, amazing....thanks for this knowledge
🤩
Good job!!! Please keep going
Thanks! These videos are just what I need!
Thank you so much Sir , I have really enjoyed watching your videos about node js
Excellent course sir!
Thank you so much Master for this video
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.
Great video, learned a lot. Keep it up!
Great
. keep going👏💫
we are missing you 😍
great explanations! thanks
brilliant job man :)
Great teaching. can someone tell why there's no content in reference site.
Great video. Why is it necessary to extend the EventEmitter class and not instanciate from it directly?
Wondering too
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 🎉
Great job
great, *absolutely* great
Great Stuff
Sir, please upload videos , your videos are very different from other node js videos available in UA-cam or any paid course
So does it make sense to replaces services with just events? for example when a user signs up, you emit the the sendEmail event.
Stellar video
you'r awesome❤
*_a m a z i n g_* lesson, whoa!
woww. Thanks soo much
Can you make more videos on node js , express js, and react js performance.
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!
amazing. thanks for it
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?
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!
@@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.
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
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.
Whenever the code that extends the EventEmitter class throws an error and emits as 'error'
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);
});
Why you remove the other videos I want that content please sir
Thanks!
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');
Your pagser page is empty, there is no list of any articles or anything.
Hi man I have some questions. Can. I ask it plais
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
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.
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.
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
}
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.
awesome
do a a video on streams
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?
thanks
I suggest renaming master object to main object
That is a great point! Thanks for mentioning it!
So, created a never ending loop
ee.on('eventThree', function() {
console.log('called');
this.emit('eventThree');
});
ee.emit('eventThree');
do you full time or you are a freelancer , just asking for a freind
it was not same. once was getting called five times by event.emit('bar')
I'm JS/TS dev but I'm in doubts node has a future. go and rust it will take all....
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
Rust will certainly take over. The laws of physics are being written in it as we speak.
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