Alexander Salas Bastidas
Coder with good taste, workaholic and ubergeek 👨🏻💻
Building realtime apps with RethinkDB and Angular.
Collaborative apps
Multiplayer games
Realtime marketplaces
Streaming analytics
+---------------------------+ | Browser / single page app | | (Angular) | +---------------------------+ ^ | +-----------------------+ | Web Server | | (Node.js/Python/Ruby) | +-----------------------+ ^ | +----------+ | Database | +----------+
Node.js + WebSockets + Angular
What about the data layer?
Database polling → slow, unscalable
Message bus → code duplication, maintenance hell
Replication log → hard to scale, lots of custom work
r.table('gameplays')
.orderBy({ index: r.desc('score') })
.limit(3)
.run(conn)
[{ player: 'coffeemug', score: 500 },
{ player: 'kittybot', score: 450 },
{ player: 'mikeg', score: 410 }]
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 }
},
...
]
// 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);
});
});
});
// 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;
<... ng-repeat='player in leaders'>
{{player.name}}
{{player.score}}
Pushes JSON updates to your app in realtime
Makes building scalable realtime apps dramatically easier
Over 35,000 developers
Second fastest growing DB on GitHub
Tweet about us! (@rethinkdb)
By Alexander Salas Bastidas
Campus at Vinco Orbis