jueves, 1 de marzo de 2012

[en] node.js + socket.IO + mongoDB = real time apps Part 1

Part 1: Setting the environment ....

A few days ago I finished a simple application tweetland.com.ar I made in order to learn more about Node.js and MongoDB ... on the road I also came across Socket.IO which is a great library for real time applications.
The first is to prepare the environment. To do this first download node.js (from node.js ), after downloading the installation follows the traditional steps:
make install *
* the last step as root Once installed, continue with the installation of modules. For this node has npm (which is Node Package Manager) that allows us to install the modules with a single command install npm . Instructions for installing npm are in your home page, also I leave
curl http://npmjs.org/install.sh | sh
At this time we have installed both node.js as its package manager npm. Usually what I usually do is create a directory in my case I call nodeapps as workspace.
Entering the same:
cd "path to nodeapps"
and within it begin to install the modules you require. This is important because when you install the modules npm node_modules create the directory (if not there before), and install the modules in that directory.
This is important because when we do within our code something like:
var sys = require('sys');
node will automatically search the directory sys module within node_modules in the current directory and not finding it will continue with the search recursively upwards, which is always important to remember where you installed the modules that we use.

Before going further make the installation of some modules used, these are express, socket.io, mongoose.

And this is precisely the last module we will use to interact with our database MongoDB, which is an object-oriented database (JSON), high performance and scalable. (in another post I will explain a little more how it works because it is a relatively new and very interesting).
To install you have to download the appropriate version from download , and once discharged, proceed to decompress

 pepo@sokol:~/DESARROLLO/POSTS/node_socket_mongo$ wget http://fastdl.mongodb.org/linux/mongodb-linux-i686-2.0.2.tgz

2012-02-28 11:42:19 (1.06 MB/s) - `mongodb-linux-i686-2.0.2.tgz' saved [38157227/38157227]

pepo@sokol:~/DESARROLLO/POSTS/node_socket_mongo$ tar -xzf mongodb-linux-i686-2.0.2.tgz
Once unpacked and could start the service mongod and start, but I'd rather create the directory structure I will use my base, create two directories for data and logs, the first store the files of the database and the second of logs. I also create the configuration file with which I will start the service, in my case is as follows:
cat mongod.config 

dbpath = /home/pepo/nodeapps/data/
logpath =/home/pepo/nodeapps/logs/mongodb.log
bind_ip =
noauth = true 
verbose = true 
 Bind_ip option is set to only listen on localhost requests (since by default it does on all interfaces) and indicate that we will not configure the authentication and verbose.

To start the service simply run:
bin/mongod --config mongod.config *
* Inside the unzipped directory MongoDB

we see something like this:
 # mongodb-linux-i686-2.0.2/bin/mongod --config mongod.config 
all output going to: ./logs/mongodb.log
Finally to test the service entered as follows (always from the binary directory MongoDB):
pepo@sokol:bin$ ./mongo
MongoDB shell version: 2.0.2
connecting to: test
> show dbs
local    (empty)
Well, with this we have our environment set up and configured to start programming applications in node.js with MongoDB, but that will be for the next post....

1 comentario:

  1. Hi Javier,

    Really like to see your approach with mongoDB, Node.js, and Socket.io to build a real-time or near real-time app to deal with the live stream tweets stored in MongoDB.
    Is there any part two out there yet?