Chapter 5. Programming RethinkDB in Node.js

In the previous chapters, you learned to interact with RethinkDB by running queries through the web interface; however, while this can be useful for testing purposes, it's not very efficient when using the database for production purposes.

If you're working on a web application, you typically want to incorporate the database programming within your application logic usually on the backend. This is one of the reasons why databases offer drivers, which are modules that allow you to access the database from your favorite programming language. Probably, the two most common languages used for backend programming are Python and Node.js, and you'll be happy to know that RethinkDB provides official drivers for both these languages.

In this chapter, we will focus on Node.js and understand how to interface it with RethinkDB.

In this chapter, you will also learn the following:

  • How Node.js works and why it's so popular
  • How to install RethinkDB's Node.js driver
  • How to run queries from Node.js

At the end of the chapter, you'll also learn about Changefeeds, one of the most popular features in RethinkDB, which make it perfectly suitable for the development of real-time web applications.

Introducing Node.js

This chapter focuses on using Node.js to interact with RethinkDB; however, although you may have used JavaScript before, you maybe interested in knowing a little more about how this new technology works.

First of all, Node.js is a software framework built upon Chrome's JavaScript runtime (called V8) that can be used to develop fast and scalable web applications. Node is provided as a full-fledged platform as it offers a high-performance web development framework that can be programmed in JavaScript.

You may have noticed the presence of two keywords in the previous paragraph: scalable and high-performance. The reason for this is that since the very beginning, Node.js has focused on the performance, and this has been possible due to the way that Node is designed. In fact, what sets Node.js apart from traditional web servers and applications is its event-driven, non-blocking I/O model that makes it lightweight and efficient and perfectly suited for real-time applications that run across distributed machines.

Note

This chapter is not meant to be a complete reference to Node.js, so it will not cover every detail of Node.js APIs. This chapter tries to cover just enough to get you started with programming RethinkDB with Node.js.

An increasingly popular technology

Node.js is a relatively new platform and is still evolving; however, it's already had a huge impact on the way we develop applications for the web, and it is becoming more popular by the day. You maybe surprised to know that Node is currently powering web services for some of the Internet's largest companies. What are the reasons behind its success?

The first and most important advantage of Node.js is JavaScript. If you know how to program in JavaScript and you're comfortable with its syntax, you already know how to program in Node.js. The only thing you need to do is learn all about APIs and modules. Node.js is based on simplicity—the core functionality is kept to a minimum, and the existing APIs are simple and easy-to-use. If you want to build something more complex, you can easily choose, download, and install any of the third-party modules.

If you think about it, the fact that Node is based on JavaScript is highly convenient because we've been programming software frontends in JavaScript for years, and now, we can build an entire web application, backend and frontend, in JavaScript. The cherry on the cake is that we can also use JavaScript to interact with the database now.

One of the other reasons that have contributed to making Node.js so popular is that many large companies have invested heavily in Node in the past years. Starting from Joyent, many other companies have invested and built software on Node, assuring it a stable future.

An event-driven design

Since the very beginning, Node.js has focused on the performance, and this is reflected in the way it is designed. Typical network applications are based on "blocking" operations and multithreading. In practice, what this means is that in the traditional network programming, the processing of a request cannot continue until the previous operation finishes.

The problem with this programming model is that it doesn't scale well, so the problem was originally solved with multithreaded programming. Threads are light-weight processes that share memory with all other threads in the same process, and a network application based on this model means that when a thread is waiting for an I/O operation to complete, another thread can start serving a new request. The problem with this type of approach is that, although it is much more efficient than traditional single-threaded programming, creating new threads adds a great layer of overhead. This is particularly evident when you need to handle thousands of concurrent connections. Node.js to the rescue!

Node.js adapts a different model called event-driven programming. In this model, the flow of execution is determined by events. Events are handled by event handlers and callbacks functions that are invoked as a response to an event. If you already know how to program in JavaScript, you may already be familiar with the concept of listening for events. Node.js uses this same approach in every aspect of its platform. In practice, I/O operations, server requests, and interactions with a database will all be handled using a callback function attached to an event listener.

If you don't have any previous experience with Node.js or event-driven programming, this approach may seem confusing at the beginning. Let's try to clarify it with an example.

Consider how a sample query is executed in traditional blocking programming to a database:

var queryResult = database.query("Example query");
do_something_with_result(queryResult);

As you can see from this example, this query requires the thread or process that runs the query to finish processing it before saving the result.

In an event-driven environment, this query can be performed like this:

var queryFinished = function(result) {
    do_something_with_result(result);
};
database.query("Example query", queryFinished);

The first thing defined is what will happen when the query has finished. This is saved as a function in the queryFinished variable, but it could also be provided as an inline callback function. This variable is then passed as an argument to the query function. In practice, when the database finishes processing the query, the queryFinished function will automatically be called.

As you can see from this simple example, instead of using a return value, we define a function that is called when something interesting happens—in this case, when the query completes. This style of programming is called the event-driven or asynchronous programming and is at the base of Node.js.

By now, you must have understood why Node.js is such a popular choice when developing web applications; being based on an event-driven design means that the Node.js process will never block when it performs I/O and, more importantly, multiple I/O operations can occur in parallel without using threads.

Note

Node.js is not the only platform that is based on an event-driven design; there are several other frameworks that use this programming model, and probably, the two most popular ones, excluding Node.js, are Python's Twisted and Ruby's Event Machine; however, developing a web application using one of these frameworks requires specific knowledge. Node.js, on the contrary, requires no previous knowledge on event-based programming and is far more intuitive and easy to use than other frameworks.

The fact that Node.js works in an asynchronous manner makes it a perfect match for RethinkDB, as the combination of these two technologies allow us to develop web applications with the capability of handling a very high load and exploiting some of RethinkDB's unique features, such as Changefeeds, which we will take a look at further on in this chapter.

Now that we know a little more about Node.js, it's time to install it and start using it to run queries on our database!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.225.188