Tuesday, September 23, 2014

NodeJs mongoDB : With sample application

Hi,

NodeJs connectivity with the MongoDB Database.
Here, i am assuming that you have NodeJS, NPM and socket.io installed on your machine. If not kindly follow the below URL to install the above tools:
http://tarunlinux.blogspot.in/2014/09/nodejs-basics.html

Lets start to create sample appication on NodeJs with mongoDB connectivity.

Step 1: Install mongoDB on your machine... Please follow the below link to setup mongoDB..
http://tarunlinux.blogspot.in/2014/09/mongodb-basics.html

Step 2: Install NodeJS mongoDB dependency with the below command:

1
# sudo npm install mongodb



It will install mongodb module in your application.

Step 3: Create server.js file for the nodejs:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
var mongo = require('mongodb').MongoClient;
var client = require('socket.io').listen(8080).sockets;  //Load socket.io module

mongo.connect('mongodb://localhost/tarundb', function(err, db){
    client.on('connection', function(socket){
        var col = db.collection('employee'); //connect with collection of tarundb

        //Insert data into mongodb
        col.insert({name: 'Tarun', age:26}, function() {
            console.log("Inserted");
        });
        
        //emit all the records
        col.find().limit(100).toArray(function(err, res){
            if(err) throw err;
            
            socket.emit('all-data', res);
        });
    });
});


Step 4: Create Index.html file for the frontend:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<html>
    <head>
        <title> NodeJs MongoDB APP </title>
    </head>
    <body>
        <div class="data-set"></div>
        <script src="http://localhost:8080/socket.io/socket.io.js" type="text/javascript"></script>
        <script src="http://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
        <script>
        (function(){
            try{
                var socket = io.connect('http://localhost:8080');
            } catch(e){
                console.log(e);
            }
            
            if(socket !== undefined) {
                socket.on('all-data', function(data){
                    for(var i=0; i < data.length; i++) {
                        $(".data-set").append('<li>'+ data[i].name +'</li>');
                    }
                });
            }
        })();
        </script>
    </body>
</html>


Step 5: Now you have to run server.js file
# nodejs server.js

Then open your browser, with url: http://localhost:8080/
Where you will see the mongoDB connectivity and their data.

I hope it will help you for NodeJs Connectivity with MongoDB.. If you have any query, please put your comments, i highly appreciate that ...

Bye..

Monday, September 22, 2014

NodeJS MySql Connectivity : With Sample Application

Hi,

Here, i will share with you on NodeJS sample application with MySql Database connectivity.

I am assuming that  you already installed the NodeJS, NPM and Socket.io on your machine.
If not, you can follow below link to install the above requirements
http://tarunlinux.blogspot.in/2014/09/nodejs-basics.html

Here, I will show you in step-wise :

Step 1: Install the mysql module... here also i am assuming that mysql is already installed in your machine.
Now install mysql module for the NodeJs dependency.
# sudo npm install mysql

That will install the MySql dependency in your machine.

Step 2: Now Create the server.js file for the nodejs:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
var mysql = require('mysql'); // load mysql module
var client = require('socket.io').listen(8080).sockets;  //Load socket.io module

//Database connectivity
var db = mysql.createConnection({
    host: 'localhost'
    , user: 'root'
    , password: 'root'
    , database: 'zfskel'
    , port: '3306'    
});

db.connect(function(err){
    console.log(err);
});

//Create connection with the client and emit data on socket
client.on('connection', function(socket){
    db.query('SELECT * FROM employee')
      .on('result', function(data){

        socket.emit('all-data', [data]);
    });
});


Step 3: Create Index.html file for the front-end:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<html>
    <head>
        <title> NodeJs Mysql APP </title>
        <link rel="stylesheet" href="">
    </head>
    <body>
        <div class="data-set"></div>
    <script src="http://localhost:8080/socket.io/socket.io.js" type="text/javascript"></script>
    <script src="http://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
    <script>
    (function(){
        try{
            var socket = io.connect('http://localhost:8080');
        } catch(e){
            console.log(e);
        }
        
        if(socket !== undefined) {
            socket.on('all-data', function(data){
                for(var i=0; i < data.length; i++) {
                    $(".data-set").append('<li>'+ data[i].name +'</li>');
                }
            });
        }
    })();
    </script>
    </body>
</html>



Now your have 2 files i.e. server.js and index.html file.

Step 4: Now you need the sql file for the database connectivity:

1
2
3
4
5
6
7
CREATE TABLE `employee` (
  `id` int(11) NOT NULL,
  `name` varchar(45) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `employee` VALUES (1,'tarun',26),(2,'varun',34);


Step 5: Now you have to run only server.js file form command line:
# nodejs server.js

Then open your browser and run the below url:
http://localhost:8080/

Here, you will see the MySql data on your browser...

I hope it will help you to understand NodeJs and its connectivity with the MySql connectivity.

Bye.. :)

NodeJS Basics

Hi,

I like to share some information on NodeJs.

What is NodeJs.
NodeJs is a cross-platform runtime environment for server-side and networking applications.
Node.js applications are written in JavaScript, and can be run within the Node.js runtime on OS X, Microsoft Windows and Linux with no changes.
Node.js internally uses the Google V8 JavaScript engine to execute code, and a large percentage of the basic modules are written in JavaScript. 

In this section, I will tell you on the below points:
1. How to install NodeJs
2. How to install NPM
3. How to install Socket.io
4. How to run nodejs from CLI

1) Installation:
I install the NodeJs on Ubuntu 14.04 O.S with the below command:

# sudo apt-get install nodejs

2) Install NPM(Node Package Manager):

# sudo apt-get install npm

Here, NPM basically a package manager.
NPM is helpful to maintain the NodeJS  dependency in your development.

3) Install Socket.io
Basically it is useful to maintain the socket connection with your frontend and backend. Or you can say..
Its provide you a real time communication between your node.js server and clients.
Installation command:
# sudo npm install socket.io

4) Run the NodeJs:
Applications are executed from the command line with the command:

# nodejs <application name>.js

In my next article, i will demonstrate your how to use it in real time communication.

Thursday, September 18, 2014

mongoDB Basics

Hi,

From last few days i started on mongoDB. It looks interesting as i never work on noSQL type database. Before that i worked on SQL, MySQL and Oracle Database.

mongoDB is the different one from the above DBs. Here, i like to share with you a quick intro on mongoDB.

mongoDB: is a cross-platform document-oriented database.
Classified as a NoSQL database, MongoDB eschews the traditional table-based relational database structure in favor of JSON-like documents with dynamic schemas (MongoDB calls the format BSON)
making the integration of data in certain types of applications easier and faster.

I use the mongoDB in one of my project... I installed this on my machine Whose OS is Ubuntu 14.04 version.

Installation
# apt-get install mongodb
It will install the client and server and other library associated to it.

Open the port in mongod.conf,
# sudo vi /etc/mongodb.conf
Just uncomment the below line in conf file
port = 27017

Start of mongoDB
# sudo service mongodb start

Its a pretty simple to setup on Ubuntu... :)

mongo is a part of the standard mongoDB distribution and provides a full JavaScript environment with complete access to the JavaScript language and all standard functions as well as a full database interface for mongoDB.

Now Start the mongo
# mongo
It will take you on mongo prompt....

Some basic commands for mongoDB

> db  // show current db

> show dbs //show all db with their size

> use db-name  //shitch to your selected db

> help  //show help for the mongodb

> show collections //show the collections used in your selected db

> db.collection-name.find()  // give you all records in your collection

> db.collection-name.insert({name:'Tarun', message:'Welcome'})  //insert record into the collection

> db.collection-name.find({name:'Tarun'})  //give the particular record from your collection
Here, important point is, this is case-sensitive.
if your search as
> db.collection-name.find({name:'tarun'})  // it will not show records whose name as Tarun  

> db.collection-name.findOne()  //to get the one document from the collection
 
> db.collection-name.find().limit(3)  // to get 3 document from all document


Thursday, September 11, 2014

Docker : Container Technology

Hi,

This is on the new technology that i listened from someone and that makes some curiosity inside me.

After some search and digging on Google then i found some basic informations. Those information that i want to share with all blogger readers guys.

First is, What is Docker?
Why its required?
Why it's different from VM?
Then How can i use it in my activity?

Huuuu... Many questions..... :)

Lets start to know each and every question with their some basic information...

Now, Question 1: What is Docker?
Ans: Docker is basically a new container technology where developers or admin team can manage the application component with their desired versions in one place that says as container, after that they are able to ship that container at any place or say any machine without affecting machines configurations.

Or in Nutshell
providing an additional layer of abstraction and automation of operating system–level virtualization.

Question 2:  Why its required?
Ans: I thing previous ans is self explanatory for this one.
In further, With Docker, developers can build any app in any language using any tool chain. “Dockerized” apps are completely portable and can run anywhere.

Question 3:  Why it's different from VM(Virtual Machine)?
Ans:  
Virtual Machine: Each virtualized application includes not only the application - which may be only 10s of MB - and the necessary binaries and libraries, but also an entire guest operating system - which may weigh 10s of GB.

Docker: The Docker Engine container comprises just the application and its dependencies. It runs as an isolated process in userspace on the host operating system, sharing the kernel with other containers. Thus, it enjoys the resource isolation and allocation benefits of VMs but is much more portable and efficient.

Question: Then How can i use it in my activity?
Ans: I try to put some basic information on:
How to install it on your machine?
What are the basic command to use the docker?


INSTALLATION Process

Installation of the Docker in ubuntu 14.04 :

# sudo apt-get update

# sudo apt-get install docker.io

# sudo ln -sf /usr/bin/docker.io /usr/local/bin/docker

# sudo sed -i '$acomplete -F _docker docker' /etc/bash_completion.d/docker.io

# sudo docker run -i -t ubuntu /bin/bash

Above command will take you in bash prompt, where you can install the software/application that you want in the container.

Update the container package list:
# apt-get update
# apt-get install apache2   // install the apache2 in your container
# apt-get install mysql-server mysql-client
//install the mysql-server and mysql-client

# apt-get install php5 php5-mysql libapache2-mod-php5
// install php on the container

To exit from the docker:
# exit 
//It will take out you from docker prompt.

If you want to have particular distribution then use:
# docker run -i -t ubuntu:12.04 /bin/bash
It will create the ubuntu12.04 container.



Basic Command to get the docker information:

1. To know the docker version
# docker version

2. To search the specific docker image
docker search <image-name>
EX:
# docker search tutorial

3. To download the docker specific container image
docker pull <username>/<repo-name>
EX:
# docker pull tarunsinghal/lampstack

4. To run the docker container with the some output
docker run <username>/<repo-name> echo "Hello Tarun"
EX:
# docker run tarunsinghal/lampstack echo "Hello Tarun"

5. To run the any install in docker container
docker run <username>/<repo-name> apt-get install -y ping
EX:
# docker run tarunsinghal/lampstack apt-get install -y ping

6. To commit the docker images changes
docker commit <id> <username>/<repo-name>
EX:
# docker  commit 6789 tarunsinghal/lampstack 

Here,  id: is the docker id which will be identified by the
# docker ps -a   // that shows you number of container with their ids.
if you pass ids starting 4 character then thats enough to identify the right container.

7. To run the docker installed utility
docker run <username>/<repo-name> ping google.com
EX:
# docker run tarunsinghal/lampstack ping google.com

8. To know the running docker information with specific id.
# docker ps -a  // will tell you the running docker(s)
# docker inspect <id-number>  // will give you complete information on this docker ids

here, id : is the docker container id number

9. To push the new image in the docker registry
# docker push <username>/<repo-name>

In this way, you downloaded the docker with given repo, after that you make some changes and push it again to the docker registry with new name.

Now, that new docker images will be usable in any other similar environment.

10. If you want docker will automatically on when you boot your machine
# docker chkconfig docker on

Further more, if you want to know on this you can refer the below URL for Docker:  https://www.docker.com

Wednesday, September 3, 2014

PHPUnit Pear Install In Ubuntu

Hi,

To install the PHPUnit in ubuntu, Please follow the below instruction:

If your system does not have Pear installed then:

# sudo apt-get install php-pear

# sudo pear channel-update pear.php.net

Now, You have to install the PHPUnit, with the help of pear easily:


# sudo pear channel-discover pear.phpunit.de

# sudo pear install -a phpunit/PHPUnit

If you want the code-coverage analysis of your application then you have to install the PHP extension i.e. Xdebug , That will help you create the html formatted report where you can see:
i) Code coverage percentage
ii) Which test case run on your application code

Command, to install Xdebug:
# sudo apt-get install php5-xdebug

NOTE: The above process of installation via PEAR would be outdated on Dec, 2014.
Please try to use Composer instead of PEAR