Node js is a single threaded application but it support concurrency via concept of event and callbacks. As every API of Node js are asynchronous and being a single thread, it uses async function calls to maintain the concurrency. Node uses observer pattern. Node thread keeps an event loop and whenever any task get completed, it fires the corresponding event which signals the event listener function to get executed.
Event Driven Programming
Node.js uses events heavily and it is also one of the reasons why Node.js is pretty fast compared to other similar technologies. As soon as Node starts its server, it simply initiates its variables, delcares functions and then simply waits for event to occur.In an 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.
While Events seems similar to what callbacks are. The difference lies in the fact that callback functions are called when an asynchronous function returns its result where as event handling works on the observer pattern. The functions which listens to events acts as Observers. Whenever an event gets fired, its listener function starts executing. Node.js has multiple in-built events available through events module and EventEmitter class which is used to bind events and event listeners as follows:
// Import events module var events = require('events'); // Create an eventEmitter object var eventEmitter = new events.EventEmitter();
Following is the syntax to bind event handler with an event:
// Bind event and even handler as follows eventEmitter.on('eventName', eventHandler);
We can fire an event programatically as follows:
// Fire an event eventEmitter.emit('eventName');
Example
Create a js file named main.js having the following code:// Import events module var events = require('events'); // Create an eventEmitter object var eventEmitter = new events.EventEmitter(); // Create an event handler as follows var connectHandler = function connected() { console.log('connection succesful.'); // Fire the data_received event eventEmitter.emit('data_received'); } // Bind the connection event with the handler eventEmitter.on('connection', connectHandler); // Bind the data_received event with the anonymous function eventEmitter.on('data_received', function(){ console.log('data received succesfully.'); }); // Fire the connection event eventEmitter.emit('connection'); console.log("Program Ended.");
Now let's try to run the above program as check the output:
$ node main.js
This will produce following result:
connection succesful. data received succesfully. Program Ended.
How Node Applications Work?
In Node Application, any async function accepts a callback as a last parameter and the callback function accepts error as a first parameter. Let's revisit the previous example again. Create a text file named input.txt having following contentThis Blog is provide self learning technique to learn the things in simple and efficience way.
Create a js file named main.js having the following code:
var fs = require("fs"); fs.readFile('input.txt', function (err, data) { if (err){ console.log(err.stack); return; } console.log(data.toString()); }); console.log("Program Ended");
Here fs.readFile() is a async function whose purpose is to read a file.
If an error occurs during read of file, then err object will contain the
corresponding error else data will contain the contents of the file.
readFile passes err and data to callback function after file read
operation is complete, which finally prints the content.
Programe Ended
This Blog is provide self learning technique to learn the things in simple and efficience way.
very nice blog thanks for the valuable information
ReplyDeleteFull Stack online Training
Full Stack Training
Full Stack Developer Online Training
Best article, very useful and explanation. Your post is extremely incredible. Thank you very much for the new information.
ReplyDeleteNode Js Online Training in Hyderabad
Node Js course with placement in Hyderabad
Node Js course in Hyderabad with placement
Node Js Training Center in Hyderabad
I really enjoyed your blog Thanks for sharing and it was very useful for everyone
ReplyDeleteNode JS Online training
Node JS training in Hyderabad
Best article, very useful and explanation. Your post is extremely incredible. Thank you very much for the new information.
ReplyDeleteNode Js Training Center in Hyderabad
Node Js course with placement in Hyderabad
Node Js course in Hyderabad with placement
ReplyDeleteThanks for sharing valuable information. Your blogs were helpful..
Mean stack online training
Mean stack training in hyderabad
This post will be very useful to us.
ReplyDeleteNode JS Online Training