Wednesday 20 June 2012

SQLFire tutorials - 1


SQLFire is a memory-optimized, distributed database management system designed for applications that have demanding scalability and availability requirements. SQLFire offers some of the best features usually only seen in NoSQL databases, such as horizontal scalability, shared-nothing persistence and built in fault tolerance, but does it all while providing a real SQL interface. Applications can manage database tables entirely in memory, or they can persist tables to disk to reload the data after restarting the system. A SQLFire distributed system can be easily scaled out using commodity hardware.

In this post we’ll see how to setup and start a cluster of multiple SQLFire servers. Read more..

The canvas element – Basic usage

The HTML5 Canvas element has its contents rendered with JavaScript. The canvas element makes use of the "context" into which you can issue JavaScript commands to draw anything you want. The canvas is initially blank, and to display something a script first needs to access the rendering context and draw on it. You can obtain the rendering context and it's drawing function by using the getContext method. Read more..

Monday 16 April 2012

Node.js: Templating with Plates


Plates is the templating library in flatiron. Plates binds data to markup. It's Javascript, Markup and JSON. It works in the browser and in node.js. All templates are actually valid HTML, with no special characters for value insertion. The relationship between tags and values is defined through object literals that are passed to Plates.bind:
You have to install plates before using it


Plates can be used in the program by requiring it.
var http = require('http'),
    plates = require(../lib/plates');

var server = http.createServer(function (req, res) {
    res.writeHead(200);
    var userSpan = '<span id="spFirstName">Prajeesh</span>&nbsp;<span id="spLastName">Prathap</span>'
    var userHtml = '<div id="pnlLogin"></div>';

    var map = plates.Map();
    map.where('id').is('spFirstName').use('value').as('Prajeesh');
    var content = plates.bind(userHtml, { 'pnlLogin': userSpan });
    res.end(content)
});


server.listen(8083);


Sunday 15 April 2012

Routing using director in Node


Director is a URL router module which comes as part of the flatiron framework.  It works in a browser for single page apps and in Node.js. It's not a plugin for another framework. It's not dependent on anything. It's a modern router that was designed from the ground up with javascript.
Installing flatiron
You can install flatiron using npm (npm install flatiron –g)

Director handles routing for HTTP requests similar to journey or express:
var http = require('http');
var director = require(../lib/director');

function sayWelcome(route) {
    this.res.writeHead(200, { 'Content-Type': 'text/plain' });
    this.res.end('Welcome to flatiron routing sample');
}

var router = new director.http.Router({
    '': {
        get: sayWelcome
    }
});

router.get('/index', sayWelcome);
router.get('/home', sayWelcome);

var server = http.createServer(function (req, res) {
    router.dispatch(req, res, function (err) {
        if (err) {
            res.writeHead(404);
            res.end('Error!!!');
        }
    })
});

server.listen(8083);
When developing large client-side or server-side applications it is not always possible to define routes in one location. Usually individual decoupled components register their own routes with the application router. Director supports ad-hoc routing also.
router.path(/\/customers\/(\w+)/, function (res) {
    this.post(function (id) {
        //sayWelcome('some');
        this.res.writeHead(200, { 'Content-Type': 'text/plain' });
        this.res.end('Create a customer with Id = ' + id)
    });

    this.get(function (id) {
        //sayWelcome('some');
        this.res.writeHead(200, { 'Content-Type': 'text/plain' });
        this.res.end('Gets a customer with Id = ' + id)
    });
    this.get(/\/orders/, function (id) {
        this.res.writeHead(200, { 'Content-Type': 'text/plain' });
        this.res.end('Gets orders for a customer with Id = ' + id)
    });
});















Tuesday 10 April 2012

Exception handling in Node


A common problem when writing Node.js programs is in ensuring all exceptions are handled. Node.js will helpfully die when exceptions are not caught and handled, which forces you, the author, to ensure robustness in your Node.js applications.  Node emits an uncaughtException event when an exception bubbles all the way back to the event loop. If a listener is added for this exception, the default action (which is to print a stack trace and exit) will not occur.
process.on('uncaughtException', function (ex) {
    console.log('Unhandled exception caught: ' + ex);
    process.exit();
});

If you are writing asynchronous code, you can follow the below given pattern to throw and handle exceptions in the code. In this pattern the first argument of the callback is err, if an error happens it is the error, if it doesn't it is null.
var divideAsync = function (arg1, arg2, callback) {
    if (arg2 == 0) return callback(new Error('division by 0 is not possible'));
    return callback(null, arg1 / arg2);
}

divideAsync(9, 0, function (ex, result) {
    if (ex) {
        console.log('Error in division ' + ex);
        process.exit();
    }
    console.log('result ' + result);
});

You can also use the traditional try {} catch (err) {} to catch exceptions as given below
console.log('Starting directory: ' + process.cwd());
try {
    process.chdir('/tmp');
    console.log('New directory: ' + process.cwd());
}
catch (err) {
    console.log('chdir: ' + err);
    process.exit();
}

For synchronous code, if an error happens you can return an instance of Error object and later check the result for this object as given below.
var divide = function (arg1, arg2) {
    if (arg2 == 0) return new Error('division by 0 is not possible');
    return arg1 / arg2;
}

var x = divide(5, 0);
if (x instanceof Error) {
    console.log('Exception logged');
    process.exit();
}

Monday 9 April 2012

Buffers in Node – Part 1


Pure javascript, while great with unicode-encoded strings, does not handle straight binary data very well. This is fine on the browser, where most data is in the form of strings. However, node.js servers have to also deal with TCP streams and reading and writing to the filesystem, both which make it necessary to deal with purely binary streams of data. Node has several strategies for manipulating, creating, and consuming streams.

Raw data is stored in instances of the Buffer class. A Buffer is similar to an array of integers but corresponds to a raw memory allocation outside the V8 heap. A Buffer cannot be resized.

The Buffer class is a global, making it very rare that one would need to ever require('buffer').

Converting between Buffers and JavaScript string objects requires an explicit encoding method. The encodings that are supported are 'ascii', 'utf8', 'usc2', 'base64', 'binary' and 'hex'.
Writing to buffers

new Buffer(size) methods allocates a new buffer of size octets. Once the buffer is created, the buffer.write method is used to write a string value to the buffer

var buffer = new Buffer(100);
var stringToWrite = 'Sample text to add to a buffer';
//Write data to a buffer
buffer.write(stringToWrite, 'utf-8');
//finding the length of buffer
var offset = Buffer.byteLength(stringToWrite);
console.log(offset);
//Appending data to the existing buffer
buffer.write('. This is an appended text', offset, 'utf-8');
console.log(buffer.toString('utf-8'));

Copy values to buffer
buffer.copy allows one to copy the contents of one buffer onto another. The first argument is the target buffer on which to copy the contents of buffer, and the rest of the arguments allow for copying only a subsection of the source buffer to somewhere in the middle of the target buffer. For example:
var helloBuffer = new Buffer(50);
helloBuffer.write('Hello ', 0, 'utf-8')
var welcomeBuffer = new Buffer('node.js.');
var helloStringLength = Buffer.byteLength('Hello ');
var welcomeStringLength = Buffer.byteLength('node.js.');
//Copies from welcomeBuffer to helloBuffer
welcomeBuffer.copy(helloBuffer, helloStringLength, 0);
console.log(helloBuffer.toString('utf-8'));

Cropping the buffer
buf.slice([start], [end]) method returns a new buffer which references the same memory as the old, but offset and cropped by the start (defaults to 0) and end (defaults to buffer.length) indexes. The slice is not a new buffer and merely references a subset of the memory space. Modifying the slice will also modify the original buffer. For example:
var nodejsSlice = helloBuffer.slice(helloStringLength, helloStringLength + welcomeStringLength);
console.log(nodejsSlice.toString());
nodejsSlice.write('Prajeesh');
console.log(helloBuffer.toString());



Sunday 8 April 2012

Node.js application using Socket.io


Socket.io provides real-time communication between your node.js server and clients. Socket.IO allows you to emit and receive custom events. Besides `connect`, `message` and `disconnect`, you can emit custom events also.
The socket can be created by the user and used as a client (with connect()) or they can be created by Node and passed to the user through the 'connection' event of a server.

Installing socket.io on the server

Creating the server
var http = require('http'),
    io = require('socket.io'),
    sys = require('util'),
    express = require('express');

var port = 8083;
var server = express.createServer(function (req, res) {

    // Send HTML headers and message
    res.writeHead(200, { 'Content-Type': 'text/html' });
    res.end('<h1>Socket server...</h1>');
});
server.listen(port);

var socket = io.listen(server);

socket.sockets.on('connection', function (client) {
    client.on('message', function (data) {
        console.log('Message received: ' + data);
    });
    client.on('disconnect', function () {
        console.log('Disconnected from server!!!');
    });
    var intervalId = setInterval(function (message) {
        client.send(message);
    }, 500, 'From server...');
});

Client code
<!DOCTYPE html>
<html>
  <head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script src="socket/socket.io.js"></script>
    <script>
        var socket = new io.Socket();
        socket.connect('http://localhost:8083');


        socket.on('connect', function () {
            $('#status').text('Connected');
        });
        socket.on('message', function (data) {
            $('#message').text(data);
        });
        socket.on('disconnect', function () {
            $('#status').text('Disconnected');
        });
        socket.on('customEvent', function (message) {
            $('#customEvent').text(message['time']);
        });
       </script>
  </head>
  <body>
    <h1>WebSocket Test</h1>
    <h2>Status: <span id="status">Waiting</span></h2>
    Received using client.send <pre id="message"></pre>
    <hr />   
    Received using client.emit <pre id="customEvent"></pre>
  </body>
</body>