Saturday, March 7, 2015

What is nodejs?

Since long time I wanted to write about nodejs, So finally the day is came today.

Introduction
nodejs is a software by which we can use JavaScript on server side which means we can use the JavaScript for http request and can do read/write kind of operations on server.

It's not a web server like Apache, it does not do anything by itself, you have to create a server who handles about http request and response by using it's various modules.

nodejs is usually used for real time application, something like chat, audio, video etc. It is used for I/O kind of applications eg:- database reading/writing, make network connection etc. It uses the non-blocking concept for scalable network application. It uses V8 engine for great performance on server side. The V8 engine is made by google used in google-chrome browser for fast browser experience.

nodejs uses the JavaScript's asynchronous behavior(event loop), which means when you would apply the particular task for particular event and don't worry about that task, whenever that event is occurred that particular assigned task would be performed, so nodejs uses this nature of JavaScript for performance of scalable network application.
http requests for read operation

So here we would know how the nodejs increases the performance on scalable network  application.
As you can see in above image, there is multiple http requests for server santa, the request is about to read the content from database and response back to server and finally display the content in browser/computer.

With traditional way, the whole Process 1 http request for read page and response the page to server could take the long time, the Process 2 is not started until the Process 1 is finished. The RAM and it's CPU would be in idle state until the Process 1 is completed. So this blocking nature does waste the lot of RAM and CPU when large of users would come.

This kind of situation can be handled by multiple threads, However switching between threads and RAM does waste the memory and CPU.

JavaScript is single thread language, so how nodejs achieves huge performance let's see, as we already knew JavaScript has asynchronous behavior. At above figure, to start Process 2, there is no need to complete the Process 1. So second connection/http request can be made without completed first request which means the Process 2 could be started without finished Process 1.
We don't have wait here, So no extra expenses of RAM and CPU. That's how there could make a lot of concurrent connections at same time which increases the performance  on network scalable application .

Main Benefits

1) It uses the non-blocking concept.
2) It can handle lot of concurrent connections.
3) It uses the single thread for make lot of network connections.
4) Because of single thread, there is a lot of benefit on Memory and CPU of server.
5) Same context on client and server, which means we can use JavaScript both in client and server that we don't need to worry about the switching the programming language for client and server.

You can  get the nodejs from nodejs

Sunday, March 1, 2015

BrowserSync

What is browsersync?

It's a browser testing tool, When you make web pages for different browsers and different devices. Or you can say when you working on responsive  web pages, you need to check web pages on all these different devices and browsers, So testing work is really tedious when you make some changes on html/css files and test it into different devices with different browser. The tool "browsersync" makes the testing work is really easy, this is one of the best feature of browsersync.


Here some main features I have listed out.

Interaction sync
You can mirror the various user's interactions for different browsers like, On a sinlge browser, If a user click  a link, click on button or scroll the page, these all actions would be mirror on other browsers, Suppose If you perform click action on chrome browser, this action would be perform into firefox and safari automatically.

File sync
Whatever changes you made with html, css or images, this changes would be updated on browser automatically without page refresh, Suppose if there is only one tag <p >  is existing in your page, now you would add image with <img> tag then there is no need to page refresh in browser, there would automatically refresh the page and shows the image with <img > tag in browser.

Browser support,
Most of the time we have to test our web pages on different devices like computer, tablet, mobile etc. With the use of browsersync we can test the page  on a single device and the action would be mirror in other devices as well. Suppose if you are making changes on some files and testing it on a desktop computer, at the same time we can see the differences on other devices like tablet, mobile etc synchronously, so we can do perform an action on single device and mirror this action on all other devices.

Remote Debug:- You can do remote debug like, From your desktop computer  you will be able to do debug html page displaying in android phone ,  for it these separate device should be connected.

How to install?

What other things you required.

It's a module of nodejs package, For run this tools you need to installed nodejs on your computer.
After installed the nodejs you need one of these command to let run by CLI.

For install globally which means you can run browser-sync from anywhere.
$ npm install -g browser-sync

For install locally which means you can run browser-sync fom locally only.
$ npm install browser-sync --save-dev

Also you can use this tool with task runner like Grunt and Gulp.

Good Resource
https://www.youtube.com/watch?v=heNWfzc7ufQ

In future post, I will explore the answer of below questions.

How to use it?

How it works?