Now ,that you have learned about node architechture , its better time to move ahead.
First thing which we come to hear about node is ' events and event emitters'. We can say that , events are the heart of node js. So lets understand events first .
now lets have look on event emitter.
now lets see that event occourred is true or not . Here is what i got when run this piece of code to my node console.
First thing which we come to hear about node is ' events and event emitters'. We can say that , events are the heart of node js. So lets understand events first .
Events:
Somthing that has happened in our app that we can respond to.
In node we actually come across two types of events.
1. System Events
2. Custom Events
|------------------------- |-------------------------|
| | | |
| | | |
| | | |
| system events | | custom events |
| | | |
| | | |
| | | |
| | | |
-------------------------- --------------------------|
c++ core javascript core
Libuv ---------------------> Event Emitter
System Events:
System events come from c++ side of the nodejs core . The C++ Libuv library is responsible for Node’s asynchronous I/O operations and main event loop. It is composed of a fixed-size thread pool from which a thread is allocated for each I/O operation. By delegating these time-consuming operations to the Libuv module, the V8 engine and remainder of NodeJS is free to continue executing other requests.
Libuv deals with the events coming from the operating system like finished reading a file .
Custom Events:
The javascript events that one creates by himself . Event emitter in node is where we have our custom events written in javascript.
now lets have look on event emitter.
Event Emitter:
All objects that emit events are instances of the
EventEmitter class. These objects expose an eventEmitter.on() function that allows one or more functions to be attached to named events emitted by the object. Typically, event names are camel-cased strings but any valid JavaScript property key can be used.
When the
EventEmitter object emits an event, all of the functions attached to that specific event are called synchronously. Any values returned by the called listeners are ignored and will be discarded.
The following example shows a simple
EventEmitter instance with a single listener. The eventEmitter.on() method is used to register listeners, while the eventEmitter.emit() method is used to trigger the event.const EventEmitter = require('events');
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();
myEmitter.on('event', () => {
console.log('an event occurred!');
});
myEmitter.emit('event');
now lets see that event occourred is true or not . Here is what i got when run this piece of code to my node console.
and yes , it returned true saying 'an event occurred !' .
for more eager ones here is the whole meat of events at https://nodejs.org/api/events.html .
Lets understand the Level of abstraction in nodejs as how an event travels at each level.
| javascript (event emitter)
|
| c/c++ (libuv)
|
| Assembly Language
|
| Machine Language
v
for v8 details , here are the good contents https://developers.google.com/v8/intro and also visit this v8 blog http://v8project.blogspot.in/ . Download the source code and look into it. It is c++.
Lets go deep into event loops now .
Lets understand the Level of abstraction in nodejs as how an event travels at each level.
Level of abstraction:
Here is description of how events travel .| javascript (event emitter)
|
| c/c++ (libuv)
|
| Assembly Language
|
| Machine Language
v
Node is written in c++ ,not javascript
Node is written in c++. though it seems surprising that we write all the codes in javascript . Yes alright , we write all the codes in javascript say functions , methods and all , and when we require any node library , even then we use javascript . but let me tell you , Node is itself written in c++. And the main reason why node is written in c++ is because v8 , the engine is written in c++. v8 converts javascript to machine code .So node , which is onto it ,is written in c++.for v8 details , here are the good contents https://developers.google.com/v8/intro and also visit this v8 blog http://v8project.blogspot.in/ . Download the source code and look into it. It is c++.
Lets go deep into event loops now .
Event driven programming
This style of programming — whereby instead of using a return value you define functions that are called by the system when interesting events occur — is called event-driven or asynchronous programming. This is one of the defining features of Node. This style of programming means the current process will not block when it is doing I/O. Therefore, several I/O operations can occur in parallel, and each respective callback function will be invoked when the operation finishes.
Event Loops
The event-driven programming style is accompanied by an event loop. An event loop is a construct that mainly performs two functions in a continuous loop —
1.event detection and
2.event handler triggering.
In any run of the loop, it has to detect which events just happened. Then, when an event happens, the event loop must determine the event callback and invoke it.
This event loop is just one thread running inside one process, which means that, when an event happens,the event handler can run without interruption. This means the following:
➤ There is at most one event handler running at any given time.
➤ Any event handler will run to completion without being interrupted.
This allows the programmer to relax the synchronization requirements and not have to worry about concurrent threads of execution changing the shared memory state.
The event loop gives Node the capability to handle highly concurrent requests while still running “single-threaded”. In any event-driven application, there is generally a main loop that listens for events, and then triggers a callback function when one of those events is detected. Similarly, the event loop delegates I/O operations via POSIX interface while it still handles new requests and callbacks. Here is my take on how things work inside the node.js server:
the image is taken from http://abdelraoof.com/blog/2015/10/28/understanding-nodejs-event-loop/ .
The event loop simply iterate over the event queue which is basically a list of events and callbacks of completed operations. All I/O is performed asynchronously by the threads in the thread pool. Libuv component of Node.js plays a vital part in this. If an item requires an I/O the event loop simple delegates the operation to the thread pool. Event loop continues execution of items in the event queue. Once the I/O operation is complete, the callback is queued for processing.. Event loop executes the callback and provide appropriate results. Since, it’s in a loop.. The process simply keeps going.