Book Image

Mastering RethinkDB

By : Shahid Shaikh
Book Image

Mastering RethinkDB

By: Shahid Shaikh

Overview of this book

RethinkDB has a lot of cool things to be excited about: ReQL (its readable,highly-functional syntax), cluster management, primitives for 21st century applications, and change-feeds. This book starts with a brief overview of the RethinkDB architecture and data modeling, and coverage of the advanced ReQL queries to work with JSON documents. Then, you will quickly jump to implementing these concepts in real-world scenarios, by building real-time applications on polling, data synchronization, share market, and the geospatial domain using RethinkDB and Node.js. You will also see how to tweak RethinkDB's capabilities to ensure faster data processing by exploring the sharding and replication techniques in depth. Then, we will take you through the more advanced administration tasks as well as show you the various deployment techniques using PaaS, Docker, and Compose. By the time you have finished reading this book, you would have taken your knowledge of RethinkDB to the next level, and will be able to use the concepts in RethinkDB to develop efficient, real-time applications with ease.
Table of Contents (16 chapters)
Mastering RethinkDB
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface

Developing event consumers


As I have mentioned earlier, we are going to use MySQL and MongoDB to perform the synchronization. We have the data model ready for both of them, so all we need is to listen for events and, depending upon the type, perform database-related operations.

Before starting with the code, I would like to mention a little bit about the Observer design pattern because we are going to use it in the next section.

Observer pattern

The Observer pattern is a design pattern in which a subject maintains a list of its dependents or listeners and notifies them about any change. We can implement this pattern using Node.js by using the EventEmitter class.

Consider the following example:

const EventEmitter = require('events'); 
class MyStream extends EventEmitter { 
  constructor() { 
    super(); 
    this.files = []; 
  } 
  read(file) { 
    this.files.push(file); 
    this.emit('fileadded',file); 
  } 
  write() { 
    var self = this; 
    this.files.map(function(files) { 
      self...