Showing posts with label NodeJS. Show all posts
Showing posts with label NodeJS. Show all posts

Monday, October 17, 2016

process.nextTick() vs setImmediate() vs setTimeout() in node


Process management in Event loop


setTimeout() :

Definition: setTimeout is used to schedule a function  after some time in future ,i.e the function will run after given time.

Example
console.log("first") ;
setTimeout(function(){
console.log("second");
},2000); 
console.log('third');

Output: 

first
third


second

To understand setImmediate we must first understand few terms: 

Event Queue: This is a queue where all the callbacks/events gets stored while event loop is busy.

Tick: Each time the event loop takes callbacks/events from the event Queue for processing this is called tick.   


setImmediate() :-

Defination: setImmediate is used to schedule a function at end of the event queue (after all callbacks/events present at that time in event queue).

This is used for heavy CPU processing operations. These heavy operations can be scheduled at last using above functions so that other callbacks/events does not wait in the queue for completion of heavy tasks.

Example
console.log("first");
setImmediate(function(){
console.log("second");
});
console.log("third");

Output: 

first
third
second

Note: SetImmediate takes precedence over setTimeout & setImterval but not other callbacks.

process.nextTick(): - 

Defination: process.nextTick is used to schedule a function at front of event queue(before all callbacks/ event s waiting in the event queue).

This method is used when you want  to run some function in a fresh call stack like highly recursive functions.Since process.nextTick() puts the function on top of the event queue.So after completion of the current executing code , the event loop starts processing next function present in event queue which is you process.nextTick function in an fresh call stack.

Example
console.log("first");
process.nextTick(function(){
console.log("second");
});
console.log("third");

Output: 
first
third


second

Difference between setImmediate and process.nextTick:

process.nextTick adds in front of all callbacks and event loop while setImmediate adds at the end.

Example:
for(var i=0;i<3;i++){
fs.readFile("abc.mkv",function(err, data){
if(err){
console.log(err);
}else{
setImmediate(function(){
console.log("completed set immediate");
});
console.log("completed calback");
}
});
}
console.log('event emitted');

Output:

In the above example, I am reading a file in a loop for 3 times.After the file is read i.e  the callback returns , i have used setImmediate with a function that needs to be added in the event queue.This is simple function that prints some message in the console.
As you can see here that, "completed set immediate" is printed at last . This shows that   these were added at end of callback of read file.

Running the same function process.nextTick();
for(var i=0;i<3;i++){
fs.readFile("abc.mkv",function(err, data){
if(err){
console.log(err);
}else{
process.nextTick(function(){
console.log("completed set immediate");
});
console.log("completed calback");
}
});
}

Output:

As you can see in the output, completed set immediate is printed as soon the current callback execution is over.that means the process.nextTick adds in top of the event queue. 

Wednesday, October 5, 2016

Installing Specific version of NodeJs in Ubuntu using NVM


Steps For installing Specific Version of Ubuntu using NVM

In Order to install specific version of node in ubuntu we will be using a tool called NVM which stands for node version manager.

NVM helps us to install and manage multiple versions of node in a single machine.It helps to switch between different versions 

To install NVM , Run below commands in a terminal


  • sudo apt-get update
  • sudo apt-get install build-essential libssl-dev
  • curl -sL https://raw.githubusercontent.com/creationix/nvm/v0.31.0/install.sh | sh

To gain access to the nvm functionality, you'll need to log out and log back in again, or you can source the~/.profile file so that your current session knows about the changes:

source ~/.profile
Now that you have nvm installed, you can install isolated Node.js versions.
To find out the versions of Node.js that are available for installation, you can type:
nvm ls-remote
nvm install 6.0.0

You can change to what ever version you like.

Usually, nvm will switch to use the most recently installed version. You can explicitly tell nvm to use the version we just downloaded by typing:
nvm use 6.0.0

When you install Node.js using nvm, the executable is called node. You can see the version currently being used by the shell by typing:
node -v

If you have multiple Node.js versions, you can see what is installed by typing:
nvm ls

If you wish to default one of the versions, you can type:
nvm alias default 6.0.0

To remove / uninstall a version
nvm uninstall 6.0.0

To Exeute node REPL/command line with specific version
nvm run 6.0.0

In NVM, you can also use alias for setting the default version of node
nvm alias default 6.0.0.

Each version of Node.js will keep track of its own packages and has npm available to manage these.

You can learn more about the options available to you with nvm by typing:
nvm help

NodeJS installation in Ubuntu

Step-wise installation of NodeJs in Ubuntu

Note:  Tested on Ubuntu 14.10

Open Terminal in ubuntu using Ctrl+Alt+T.
Run the below commands :

sudo apt-get update
sudo apt-get install nodejs
sudo apt-get install npm

This should install your NodeJS in Ubuntu.The version installed will be v0.10.* which is old but stable version.
Use below command to verify the installation 
nodejs -v 

If the above installation does not work (as in EC2 & other cloud instances) or For newer LTS versions, try installing NodeJs using PPA.

Run below commands in the terminal.

curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
sudo apt-get install -y nodejs

The above commands will install Nodejs with 4 and above version.
In order for some npm packages to work (such as those that require building from source), you will need to install the build-essentials package too.

sudo apt-get install -y build-essential





NodeJS installation in Windows



Step-wise installation of NodeJS in windows Machines:


Download 32/64 bit latest/stable NodeJs installer according to your system compatibility. 

Note: For beginners , Download windows  64 bit  (.msi) Installer [Long Term Support]  

The above url will download node version 4.6.0 which also includes npm 2.15.9.The npm is a tool that manages all the modules which are needed by node like installation , up-gradation & removal of node modules .
Double click the installer and install with default options.
After the installation is finished, Open the command prompt and type below commands to verify the installation.

> node -v 
> npm -v

The output must give both node & npm versions like below image.



If the output are the versions , NodeJs is ready to go.

If the versions are not displayed ,check whether the node is present in environment path.If not, Add node installation directory in environment path. Default is "C:\Program Files\nodejs".

Check the above commands again .If the versions are still not displayed follow the following steps 

  1. Clear the installation 
  2. Delete installation directory - Default is  C:\Program Files\nodejs 
  3. Remove nodejs from environment path.
  4. Restart the system
  5. Install again.