Developing the Polyglot agent

As we have discussed earlier, the job of the Polyglot agent is to find out the changes in the table and then generate an event for the synchronization process.

For finding out the changes in the table, we already have the changefeed by RethinkDB, and for generating events, we can use the inbuilt EventEmitter class by Node.js.

The event emitter is the class in the Node.js; thus, to use it we need to inherit all the functions and property in our class. We can do this by using the following code:

const events = require('events'); 
class Polyglot extends events { 
  constructor() { 
    super(); 
  } 
} 
module.exports = Polyglot; 

Here we are using the ES6 extends keyword to perform the inheritance. In our constructor, we are calling the super() method to call the parent constructor, that is, the constructor of the event class.

Next, we need to assign a changefeed to our table and listen for the change. If there is a change in any of the data, we need to determine the type of change, such as add, update, or delete, and then generate the event.

Here is the function which does this:

const db = require('./db'); 
const userObject = new db(); 
assingChangeFeed() { 
    let self = this; 
    userObject.connectToDb(function(err,connection) { 
      if(err) { 
        return callback(true,"Error connecting to database"); 
      } 
      rethinkdb.table('users').changes({"include_types": true}).run(connection,function(err,cursor) { 
        if(err) { 
          console.log(err); 
        } 
        cursor.each(function(err,row) { 
          console.log(JSON.stringify(row)); 
          if(Object.keys(row).length > 0) { 
            if(!!row.type) { 
              switch(row.type) { 
                case "add": self.emit('insert',row);break; 
                case "remove": self.emit('delete',row);break; 
                case "change": self.emit('update',row);break; 
              } 
            } else { 
              console.log("No type field found in the result"); 
            } 
          } 
        }); 
      }); 
    }); 
  } 

The function performs the following tasks:

  1. Connects to the RethinkDB instance.
  2. Assigns a changefeed to the table.
  3. On the change, iterates over the data and broadcasts the event depending upon the type of operation.

To determine the type of operation, we have specified the include_types parameter in the changefeed.

To sum up the complete code, here is what it will look like:

const rethinkdb = require('rethinkdb'); 
const db = require('./db'); 
const userObject = new db(); 
const events = require('events'); 
class Polyglot extends events { 
  constructor() { 
    super(); 
    this.assingChangeFeed(); 
  } 
  assingChangeFeed() { 
    let self = this; 
    userObject.connectToDb(function(err,connection) { 
      if(err) { 
        return callback(true,"Error connecting to database"); 
      } 
      rethinkdb.table('users').changes({"include_types": true}).run(connection,function(err,cursor) { 
        if(err) { 
          console.log(err); 
        } 
        cursor.each(function(err,row) { 
          console.log(JSON.stringify(row)); 
          if(Object.keys(row).length > 0) { 
            if(!!row.type) { 
              switch(row.type) { 
                case "add": self.emit('insert',row);break; 
                case "remove": self.emit('delete',row);break; 
                case "change": self.emit('update',row);break; 
              } 
            } else { 
              console.log("No type field found in the result"); 
            } 
          } 
        }); 
      }); 
    }); 
  } 
} 
module.exports = Polyglot; 

However, this class is of no use if we don't hook it in our server. Here is the code added in the server file to start the Polyglot agent:

const polyglot = require('./polyglot'); 
new polyglot(); 

We are creating a new instance of the preceding code in our server. This will look for any changes in the table and generate an event when any data manipulation happens.

Feeling relaxed? Well, we still have a job to do. Yes, we have the Polyglot agent which will notify the change via events but what's the use if no one is listening? Right. So in the next section, we are going to do same.

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

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