Rethink Database for Creating a Realtime Web Application with Node JS

Earlier in realtime, any request for data from the server makes the app slow, unscalable and cumbersome to maintain. RethinkDB mainly focus on this drawback and makes building realtime apps easier. RethinkDB is the first open-source, scalable JSON database built for realtime web. The developer can tell RethinkDB to continuously push updated query results to application in realtime; this reduces the server load and also the time and effort necessary to build scalable realtime apps. Here there is complete steps for installing RethinkDB. This post also explains you to create a reat-time web application using RethinkDB and Node.js

What is RethinkDB:
RethingDB is NoSQL, open source, distributed document-oriented database specially built for the real-time web. It uses ReQL query language.
Read more about ReQL query language: Introduction-to-ReQL.

Why RethinkDB
We are entering into an era of the real-time web application or I should say we have already entered. With the advent of Websockets, It is possible to create a real-time application in no time.
Earlier, we used to pull data from the server in real time and show it to the user with a restriction that server load should not increase. Well here comes the RethinkDB, which reduces this step.
We can tell RethinkDB to pull data from Table whenever any new changes occur in other words, we can subscribe for changes in any table. This feature reduces a lot of server load and developer need not put a lot of effort for pulling data from the server.

Let's start off by installing rethinkDB

Installing RethinkDB
RethinkDB provides drivers for all major OS, visit https://rethinkdb.com/docs/install/ link to find out various official packages available for different.

Installing in Mac (OS X):
Go to this link and download Mac installer or you can install RethinkDB using Using Homebrew.
If you have brew installed then run the below command.

brew update && brew install rethinkdb

Installing in Ubuntu
To install RethinkDB in ubuntu run the below command in cmd one by one.

Installing in Windows
Go to this link and download windows installer. Now unzip the downloaded file and paste in your choice of the directory. Run the file and follow the screen instructions to complete the installation.

After executing the above command, If you see the below screen with some message than congratulations, you have successfully installed RethinkDB server.

Starting RethinkDB's Administrative panel
RethinkDb has it's own administrative panel just like PhpMyAdmin (where we manage our MySql database table). In RethinkDB's administrative panel we can manage our clusters and we can check our logs. To open administrative panel type below command in CMD.

rethinkdb --bind all

To access the web management console, open http://localhost:8080 in your browser. On this link, you should see the below Image,

Creating real-time web application
Assuming you have installed RethinkDB in your machine. Now let's create a basic application using Nodejs and RethinkDB, In this application we will perform CRUD operation with a real-time update as well as we will see how to filter records.
To start a new project, Type below command,

npm init

Run below command to install RethinkDB client driver.

npm install --save rethinkdb

Before we start let's take a look at our folder structure for this application. In the middleware folder,where we have our config files.

Now run npm install to install all dependencies. Or you can do it manually in a geek way.
Creating Nodejs server:
1. Create a file named as server.js to handle RethinkDB operation and to deliver static files to the client user.
2. As you can see in the below code, we are requiring node_modules in first three lines. After that, we are again requiring two files named as config.js and routes.js from middleware folder.
3. config.js: This file used to tell express where to find all static files.
4. routes.js: This file is used to handle all routes.
server.js:

5. Now create a file named as routes.js in middleware folder to handle all the routes. In the below we have 4 routes listed below,
1 / : Which needs no introduction I believe :D.
2. /addPlayer : In this route we will be inserting records.
3. /filterPlayers : In this route we will fetch records.
4. /deletePlayer : This route is ued to delete records.
routes.js:

Till now we have created our files required for server now let's create files for Front End.

Creating front-end

In front-end, we will use AnguarJs for JS and bootstrap for CSS. We will create 2 folders in the views folder named as JS and CSS.
Also, Read Learn AnguarJs tutorial.
Create home.html in views folder and write the below code.
home.html

Let's create an AngularJs script for sending a request to the server. Create a script.js file in the JS folder and write below code.
In below script, we have integrated socket.io with AngularJs by using a Factory method. Read more about Writing an AngularJS App with Socket.IO. Just below that we have created a service to run Ajax.
script.js:

Related Posts

12
comments:

RethinkDB’s realtime architecture can be compared to MongoDB’s oplog, but offers a much higher level of abstraction. RethinkDB’s feeds integrate seamlessly with the query computation engine, and allow you to subscribe to changes on query results, not just raw replication data. This architecture dramatically reduces the time and effort necessary to build scalable realtime apps.

Hi Srinivas,I am trying to update realtime in rethinkdb using change feed in node js event call which is working but when row is deleted from rethinkdb then node js program shuts down ie coming to the terminal. What I want ,if data deleted from rethinkdb ,node js event call will continue ,not to come out of the program . code is given belowsetTimeout(function() { r.db('db name').table("table name").filter({"userid": temp.new_val.userid}) .update({"status": "true"}).run(connection,function(err,result){ if(err) throw err; console.log('updated');