What is Node.JS

Node.JS is a wrapper built around high performance V8 JavaScript runtime from the Google Chrome browser. It allows you to run JavaScript code on the server. Node.JS contains a built in HTTP server library making it possible to run a web server without the use of external software such as Apache etc allowing more control over how the web server works.

V8 Engine

V8 is Google’s open source JavaScript engine. It is written in C++ and used in Chrome browser.
V8 compiles and executes JavaScript source code, handles memory allocation for objects, and garbage collects objects it no longer needs. V8’s stop-the-world, generational, accurate garbage collector is one of the keys to V8’s performance. V8 compiles JavaScript to native machine code before executing it instead of executing bytecode or interpreting it. The compiled code is additionally optimized (and re-optimized) dynamically at runtime, based on heuristics of the code’s execution profile. Optimization techniques used include inlining, elision of expensive runtime properties, and inline caching, among many others.

JavaScript is most commonly used for client-side scripting in a browser, being used to manipulate Document Object Model (DOM) objects for example. The DOM is not, however, typically provided by the JavaScript engine but instead by a browser. The same is true of V8—Google Chrome provides the DOM. V8 does however provide all the data types, operators, objects and functions specified in the ECMA standard.

V8 enables any C++ application to expose its own objects and functions to JavaScript code. It’s up to you to decide on the objects and functions you would like to expose to JavaScript. There are many examples of applications that do this, for example: Adobe Flash and the Dashboard Widgets in Apple’s Mac OS X and Yahoo! Widgets

A sample chat server using Node.JS

// The net module provides you with an asynchronous network wrapper.
// It contains methods for creating both servers and clients (called streams)
var net = require(‘net’)
// Creates a TCP server
var chatServer = net.createServer(),
clientList = []
// Emitted when a new connection is made by the client
chatServer.on(‘connection’, function(client) {
client.name = client.remoteAddress + ‘:’ + client.remotePort
client.write(‘Hi ‘ + client.name + ‘!\n’);
clientList.push(client)
//Emitted when data is received
// The argument data will be Buffer or String
client.on(‘data’, function(data) {
broadcast(data, client)
})
// Emitted when the other end of the socket sends a FIN packet.
// i.e. when a client is disconnected from the server
client.on(‘end’, function() {
clientList.splice(clientList.indexOf(client), 1)
})
})
// Sending the messages to clients
function broadcast(message, client) {
var cleanup = []
for(var i=0;i<clientList.length;i+=1) {
if(client !== clientList[i]) {
// Checks if client is still writeable
// otherwise destroy it
if(clientList[i].writable) {
clientList[i].write(message);
}
else {
cleanup.push(clientList[i]);
// Destroys the specific client
clientList[i].destroy();
}
}
}
//Remove dead Nodes out of write loop to avoid trashing loop index
for(i=0;i<cleanup.length;i+=1) {
clientList.splice(clientList.indexOf(cleanup[i]), 1);
}
}
// Listening the created server on port 9000
chatServer.listen(9000)

How to execute the code

1. Go to Control Panel\Programs
2. Choose “Turn Windows features on or off”
3. Check “Telnet client and Telnet server” and click Ok.


4. Open the command prompt and execute the code using the command
— node <<filename.js>>


5. Now open another instances of command prompt while keeping the above instance of command prompt running.
6. Now connect the new instances of command prompts to the first one. The first on will act as a server and another instances will be clients connecting to the server.


7. Start writing on one client and it will be writing the same text on another clients.

 

References

https://code.google.com/p/v8/
https://developers.google.com/v8/intro
http://en.wikipedia.org/wiki/V8_(JavaScript_engine)

Download code
chat1