Monday, March 6, 2017

Node.js


  • built on Google Chrome's Javascript Engine. (Written in Javascript)
  • Single thread application but run in concurrency support by callbacks and event.
  • Runtime environment + Javascript Library = Node.js
  • Open Source.
  • Cross platform ( run on OS X, Windows, and Linux)

Features
  • Acronymous and Event Driven 
    • All API's of Node.js Library is Acronymous with non-blocking. (Never wait for API return data)
    • E.g. Server moves to the next API after calling it and a notification mechanism of Events of Node.js helps the server to get a response from the previous API call.
  • Very Fast
  • Single Threaded but Highly Scalable 
    • Use a single threaded model with event looping.
    • Event mechanism helps the server to respond in a non-blocking way and makes the server highly scalable as  not like traditional servers (create limited threads to handle requests)
    • Node.js uses a single threaded program and the same program can provide service to a much larger number of requests than traditional servers like Apache HTTP Server.

Blocking Code

  • The program blocks until it reads the file and then only it proceeds to end the program.

  • Read From File!!!!!
    Program Ended
Non-Blocking Code
  • The program does not wait for file reading and proceeds to print "Program Ended" and at the same time, the program without blocking continues reading the file.

  • Program Ended
    Read From File!!!!

Callback 
  • is an asynchronous equivalent for a function. 
  • is called at the completion of a given task. 
  • Node.js heavy use of callbacks. All the APIs of Node.js are written in such a way that they support callbacks.
  • E.g. a function to read a file may start reading the file and return the control to the execution environment immediately so that the next instruction can be executed. Once file I/O is complete, it will call the callback function while passing the callback function, the content of the file as a parameter. So there is no blocking or wait for File I/O. This makes Node.js highly scalable, as it can process a high number of requests without waiting for any function to return results.
Event-Driven Programming
  • Node.js starts its server, it simply initiates its variables, declares functions and then simply waits for the event to occur.
  • In an event-driven application, there is a main loop that listens for events and then triggers a callback function when one of those events is detected.
  • Node.js uses observer pattern. Node thread keeps an event loop and whenever a task gets completed, it fires the corresponding event which signals the event listener function to execute.
  • Whenever an event gets fired, its listener function starts executing. 
  • Node.js has multiple in-built events (events module and EventEmitter) which used to bind events and event-listeners.
Different Event and Callback
  • Callback functions are called when an asynchronous function return its result.
  • Whereas event handling work on observer pattern.

No comments:

Post a Comment