Monday, May 25, 2015

What is node.js


Node is
1. A set of bindingisg to v8 for non browser work: sockets, files,

etc
2. Only exposes non-blocking, ashnychrous interfaces.
3. Only one thread, one call stack ( like the browser)
4. Low level features : E.G half closed TCP connection, TCP

throttling, UDP.
5. Has killer HTTP support.

Because the interface is purely non-blocking, users tend to achieve

decent concurrency without knowing what they are doing.
Non-blocking jail.

. No Mutex locks
. only one thing at a time
. no thread safety issue.
. Almost no side effects once event is emitted.

Node architect:
Node Standered liberary -- java script
node bindingis           -- c++ -- non blocking process calls
v8|thread pool|event loop -- c

Examples:

Node is command line tool. you have to download it and install it.

No dependencies other than python for the build system. V8 is

included in the distribution.
1. setTimeout(function(){
  console.log('wprld');
},2000);

console.log('hello');

A programs which prints "hello", wait 2 sec , ouputs "worlds" and

then exist.
SO node always exist after there is no other callbacks to be

completed.When the event loop has no timeouts or file descriptors

on it, it exists automatically.

example 2.
Every 500ms print "hello"
on SIGINT print "buy"
Using the special process object.
--> In node, you have this process global variable. it's kind of

the node is very process oriented.

setInterval(function() {

console.log('hello');
},500);

Process.on('SIGINT' function() {
    console.log('bye');
process.exit(0);   --> setting a callback, we are listening for the

SIGINT event on the process object.

Like process, many other objects in Node emit events.
A TCP server emits a "connection" event each time someone conects.
A HTTP upload emits a "data" event on each packet.

Event emitting is the kind of the fundamental in the node.

Example3.
A program which:
Starts a TCP server on port 8000
Send the peer a message
Colse the connection

--> This requires a module. Require is how you load a module in

node, this is the common JS module specification. So, you load the

net module, which is how u create a TCP server and do

net.createServer, it gives you a server and then you listen to the

connection event on that object. That connection event gives you a

call back with a socket which we call c here. And all you do is you

- on that callback, you do c.end, which send the argument to the

connection and cloases the connection.

net = require("net");

s=net.createServer();

net.on('connection', function(c){
c.end('hello');
});

s.listen(8000);

5 comments:

Unknown said...

Well, you don’t really have a choice. It is a physical law in the world of functional programming to start off with SICP, which is using a minimal version of Scheme.

But, I would ignore Scheme after that readIt and full understanding is complete, and go to Clojure as a real language. Reasons?

It has direct access to all of JVM and its many libraries.
It has quite good production-ready web frameworks, such as Ring.
Even though leaving lists as the canonical aggregation structure is something of heresy in the LISP world, the added structures and literal forms in Clojure is a great help when solving real problems; specifically the map (or hash…) literal form.

java training in chennai

Swethapriya said...



Truely a very good article on how to handle the future technology. After reading your post,thanks for taking the time to discuss this content.


Tooth Braces In Chennai

Unknown said...

nice reference i got from your article keep sharing more information for us. and keep update lot of things used to develop my business growth. thanks for well info.
SAT Training Centre in Chennai
SAT Coaching Chennai

Unknown said...

Thanks for the above post.I am using gmail from so long time. Now its time to change the color and background of my account. I am trying this but I think here is some problem in doing this. Its not done like you mentioned above. I will try again and then send you feedback. Thanks once again.
SAT Training Centre in Chennai
SAT Coaching Chennai

Akshaysri said...

Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
Facility Management Companies in Chennai