Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Why Node.js is Popular for Real Time Web Application Development - Core Reasons

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 552
    Comment on it

    In traditional web app users have no idea about server’s current state. But in real time web application (chat application, games) users are interact with server and other user in real time. Real time web applications need data to be shared very fast over the server and client side. Blocking I/O models are not suitable for data intensive applications like that, then comes Node.js which support non-blocking I/O model, that is main reason why node.js popular for real time web application like (chat applications, on line gaming, Live streaming). Node.js also support Web Socket, which is main technology for building real time web applications.

     

    Non-Blocking Asynchronous model

    In synchronous programming model code executing in a sequence. And blocking model block the second code to execute until first code is done. So both blocking and synchronous model are not very suitable for real time web application.

    Non-blocking, Asynchronous model which is used in Node have only one thread to respond all the request. Node.js uses event programming when our node server started it declared functions. There is single Thread event loop which has list of all the events, if event is declared then our event loop triggers a call back function.

    For example, if two requests came to server at same time to connect to data base, you can trigger first request and while first request running you can start to respond to second request

     

        

    image source: think360studio

     

     

    EXAMPLE OF BLOCKING AND NON BLOCKING

    Using the file system module is an example of synchronous model create block.txt file have some text in it, and write fallowing code in main.js file

    var fs= require(fs);
    
    var data  = fs.reasFileSync(/block.txt)
    
    console.log(data.toString());
    
    console.log(blocking code)

     

    run main js file using given command in terminal

    node main.js

     

    You can see in output prints text of block.txt first and then console.log, because console.log command is blocked until block.txt load.

     

    Now use non-blocking code in main.js
    var fs = require(fs)
    
    var data = fs.readFile(block.txt, function (err, data){
    
    if(err) return console.error(err);
    
    console.log(data.toString());
    
    });
    
    console.log (non-blocking code);

     

    Run code again with node main.js commad in terminal

     

    Output: shows that console.log prints first then text of block.txt, in this code program not wait for block.txt to load.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: