‫In this video and the next one
‫we're gonna dive into yet another important piece
‫of the Node JS architecture with JS events
‫and event-driven architecture.
‫So most of Node's core modules, like the ones
‫we already used, like HTTP, File System, and Timers
‫are built around an event-driven architecture,
‫and we can of course also use this architecture
‫to our advantage in our own code.
‫And the concept is actually quite simple.
‫So, in Node, there are certain objects called
‫event emitters that emit named events
‫as soon as something important happens in the app,
‫like a request hitting server, or a timer expiring,
‫or a file finishing to read.
‫These events can then be picked up by event listeners
‫that we developers set up, which will fire off
‫callback functions that are attached to each listener, okay.
‫So again, on one hand, we have event emitters,
‫and on the other hand event listeners that will react
‫to emitted events by calling callback function.
‫Simple, right?
‫And probably the best way to understand this
‫is to look at an example.
‫And so let's briefly understand how Node use
‫the event-driven architecture to handle server requests
‫in the HTTP module that we already used in another section.
‫So when we want to create a server,
‫we use the Create Server method
‫and save it to a Server Variable.
‫This implementation here is a bit different
‫from what we did before, but it works the exact same way.
‫Anyway, this server.on method is how we actually
‫create a listener, and in this case for the "request" event.
‫So let's say we have our server running,
‫and a new request is made.
‫The server acts as an emitter, and will automatically
‫emit an event called "request" each time that a request
‫hits the server.
‫Simple, right?
‫Then, since we already have a listener set up
‫for this exact event, the callback function that we attached
‫to this listener will automatically be called.
‫And this kind of function we already know from before,
‫it will simply send some data back to the client.
‫Now, it works this way because behind the scenes
‫the server is actually an instance of the Node JS
‫EventEmitter class, so it inherits all this
‫event emitting and listening logic from that EventEmitter
‫class, and don't worry, we're going to use it all
‫right in the next video, so that you become familiar
‫with all these logic.
‫Now, just to finish this lecture, I believe it's
‫important to mention that this EventEmitter logic
‫is called the Observer Pattern in Javascript programming
‫in general, and it's quite a popular pattern with
‫many used cases.
‫So the idea is I set there an observer,
‫in this case the event listener, which keeps waiting,
‫keeps observing the subject that will eventually
‫emit the event that the listener is waiting for.
‫And the opposite of this pattern is simply functions
‫calling other functions, which is something that
‫we're more used to actually, right?
‫But the observer pattern has been designed
‫to react rather than to call.
‫And that is because there is a huge benefit of using
‫this architecture, which is the fact that everything
‫is more de-coupled.
‫We don't have, for example, functions from the File System
‫module calling functions from the HTTP module
‫because it would be a huge mess.
‫Instead, these modules are nicely de-coupled
‫and self-contained, each emitting events that
‫other functions, even if they come from other modules
‫can respond to.
‫Also, using an event-driven architecture makes it
‫way more straight forward to react multiple times
‫to the same event.
‫All we have to do is to set up multiple listeners, right?
‫Okay and that is Node's event-driven architecture
‫in a nutshell.
‫And don't worry if this seems a bit too theoretical,
‫you will see this logic being used in many situations
‫throughout the course, starting right in the next video.