RethinkDB

Building realtime apps with RethinkDB and Angular.

Apps are becoming realtime

Collaborative apps
Multiplayer games
Realtime marketplaces
Streaming analytics

Realtime architecture

+---------------------------+
| Browser / single page app |
|         (Angular)         |
+---------------------------+
              ^
              |
  +-----------------------+
  |      Web Server       |
  | (Node.js/Python/Ruby) |
  +-----------------------+
              ^
              |
        +----------+
        | Database |
        +----------+

How do you build a realtime app?

Node.js + WebSockets + Angular

What about the data layer?

Pushing data in realtime

Database polling → slow, unscalable

Message bus → code duplication, maintenance hell

Replication log → hard to scale, lots of custom work

RethinkDB pushes JSON in realtime

Example: player leaderboard


r.table('gameplays')
 .orderBy({ index: r.desc('score') })
 .limit(3)
 .run(conn)

[{ player: 'coffeemug', score: 500 },
 { player: 'kittybot', score: 450 },
 { player: 'mikeg', score: 410 }]

Let's make it realtime!


r.table('gameplays')
 .orderBy({ index: r.desc('score') })
 .limit(3)
 .changes()  // IMPORTANT
 .run(conn)

[
  // initial data,
  { old_val: { player: 'mikeg', score: 410 },
    new_val: { player: 'karl', score: 430 }
  },
  ...
]

Live demo

Node.js code

// The browser initiates the connection
socket.on('leaders:changes:start', function(data) {

  // Construct the query
  query = r.table('gameplays')
           .orderBy({ index: r.desc('score') })
           .limit(3)
           .changes();

  // Send each change to the browser
  query.run(conn, function(err, cursor) {
    cursor.each(function(err, change) {
      socket.emit('leaders:changes', change);
    });
  });

});

Angular: controller

// The bindTable helper integrates with the Node.js code
var leaders = bindTable('leaders');
leaders.bind();

// A self-maintaining array of leaders
leaders.rows;

// Hook it up so the view can see it
$scope.leaders = leaders.rows;

Angular: view


<... ng-repeat='player in leaders'>
  {{player.name}}
  {{player.score}}

The first database for realtime apps

Pushes JSON updates to your app in realtime

Makes building scalable realtime apps dramatically easier

Download at rethinkdb.com

Over 35,000 developers

Second fastest growing DB on GitHub

Tweet about us! (@rethinkdb)

ReThinkDB

By Alexander Salas Bastidas

ReThinkDB

Campus at Vinco Orbis

  • 321