How to build real-time applications using Node.js and RethinkDB

About RethinkDB

If you need a NoSQL database which work with JSON data, has full support for realtime searching and has a mix of paradigms between SQL and NoSQL, a good option is RethinkDB.
This is a open-source database, which all JSON’s data is persisted into tables like a conventional SQL database, allowing you to run queries among multiple tables using the classic join command. But you can also persist arrays and sub-documents like you are used to do it in MongoDB, CouchDB or PostgreSQL.

There are some cool stuffs from RethinkDB like:

GeoSpartial support;

API to handle with Strings, Dates, Booleans and Documents;

Math API;

Map-reduce support;

HTTP client to catch some external data;

Changefeeds which is a realtime searching;

Index support (simple, compound and multi);

Native web admin dashboard;

Building the application

What about building something useful using RethinkDB? To explore the realtime searching, let’s build a simple global timeline using the changefeed feature to list all data in the timeline in realtime, by using Node.js, Express, Socket.IO and RethinkDB.

First, you need to install RethinkDB server, before you start writing the codes below, to install this database I recommend you to read and follow the instruction of this link http://rethinkdb.com/docs/install according to your operation system.

After you install it, run this commands below to initiate the project:

Now let’s work! To simplify things we are going to use ES6 code native from Node v6.x.x version and the backend will be a single file code for study purposes, but if you need to build a complex and well structure backend server using RethinkDB, you can see this project node-api-examples which has a list some API’s examples using some web routers and databases, there are some examples using RethinkDB with Koa, Express and Hapi.js.

Well, let’s write the backend server of our application, you can create the file index.js using the code below:

There are some important details you must know when you work with RethinkDB, first almost all functions from this module works using callbacks or using Promises, if you choose Promises you can write well structure async functions with better error handlers.

The changefeed (via r.table('messages').changes()) feature is database’s subscriber, which is a query observer and returns any modification from a table, the combination with the io.sockets.emit() allows the server send realtime data for the clients.

Now, let’s create a simple migration script to prepare the database before starts the server. This migration is very common in relational databases. Create the database.js file with the script below:

Now we are ready to start this application! But before start the server you must run in the first time, the database’s migration to create the database and table for this project, so you just need to run this command:

Conclusion

The RethinkDB is awesome NoSQL! This database can provide a fully support for realtime applications just using changefeed + socket.io, in this link you can read more about what you can do using changefeeds. Almost all functions can run using Promises which makes you write better code and you can use easily the ES7 async/await feature to simplify the promises functions too.