Share
Embed
Transcript
73 slides
-
What is RethinkDB? • Open source database for building realtime
web applications • NoSQL database that stores schemaless JSON documents • Distributed database that is easy to scale -
Built for Realtime Apps • Subscribe to change notifications from
database queries • No more polling — the database pushes changes to your app • Reduce the amount of plumbing needed to stream live updates -
Sample Document { "name": "Will Riker", "position": "Commander", "height": 193,
"birthdate": Mon Aug 19 2335, "ships": [ { "name": "USS Pegasus" }, { "name": "USS Potemkin" }, { "name": "USS Enterprise" }, ], ... } -
Introduction to ReQL • ReQL embeds natively into your programming
language • Compose ReQL queries by chaining commands -
Sample ReQL Queries r.table("users") .filter(r.row("age").gt(30)) r.table(“post") .eqJoin(“uId”, r.table(“users”)) .zip() r.table("fellowship")
.filter({species: "hobbit"}) .update({species: "halfling"}) -
Additional ReQL Features • Geospatial indexing for location- based queries
• Date and time functions for time data • Support for storing binary objects • Execute http requests using r.http -
Changefeeds • The changes command returns a cursor that receives
updates • Each update includes the new and old value of the modified record -
Changefeeds r.table("users").changes() r.table("users") .insert({name: "Bob"}) Changefeed output: { new_val: {
id: '362ae837-2e29-4695-adef-4fa415138f90', name: 'Bob', ... }, old_val: null } -
Changefeeds r.table("users").changes() r.table("users") .filter({name: "Bob"}).delete() Changefeed output: { new_val: null,
old_val: { id: '362ae837-2e29-4695-adef-4fa415138f90', name: 'Bob', ... } } -
Changefeeds r.table("users").changes() r.table("users") .get("362ae837-2e29-4695-adef-4fa415138f90") .update({name: "Bobby"}) Changefeed output: { new_val:
{ id: '362ae837-2e29-4695-adef-4fa415138f90', name: 'Bobby' }, old_val: { id: '362ae837-2e29-4695-adef-4fa415138f90', name: 'Bob' } } -
Changefeeds r.table("players") .orderBy({index: r.desc("score")}) .limit(3).changes() Track top three players by
score Chain the changes command to an actual ReQL query: -
What we need • Add http enpoints for getting and
adding messages • Create an Angular factory for messages • Setup changefeed in Node.js app • Add realtime listener in factory -
What we need • Add http endpoints for getting and
adding messages • Create an Angular factory for messages • Setup changefeed in Node.js app • Add realtime listener in factory -
What we need • Add http endpoints for getting and
adding messages • Create an Angular factory for messages • Setup changefeed in Node.js app • Add realtime listener in factory -
What we need • Add http endpoints for getting and
adding messages • Create an Angular factory for messages • Setup changefeed in Node.js app • Add realtime listener in factory -
HTTP endpoints with Node.js var express = require('express'); var app
= express(); var server = require('http').Server(app); app .post('/message', function (req, res) { // ... }) .get('/message', function (req, res) { // ... }); server.listen(8000); Create HTTP routes for messages -
var express = require('express'); var app = express(); var server
= require('http').Server(app); app .post('/message', function (req, res) { // ... }) .get('/message', function (req, res) { // ... }); server.listen(8000); Declare POST endpoint HTTP endpoints with Node.js -
app .post('/message', function (req, res) { return r.table('messages').insert({ text: req.params.text,
email: req.params.email, created: (new Date()).getTime() }).run(r.conn); }) .get('/message', function (req, res) { // ... }) Get `messages` table HTTP endpoints with Node.js -
app .post('/message', function (req, res) { return r.table('messages').insert({ text: req.params.text,
email: req.params.email, created: (new Date()).getTime() }).run(r.conn); }) .get('/message', function (req, res) { // ... }) `insert` new document HTTP endpoints with Node.js -
app .post('/message', function (req, res) { return r.table('messages').insert({ text: req.params.text,
email: req.params.email, created: (new Date()).getTime() }).run(r.conn); }) .get('/message', function (req, res) { // ... }) Run the query HTTP endpoints with Node.js -
var express = require('express'); var app = express(); var server
= require('http').Server(app); app .post('/message', function (req, res) { // ... }) .get('/message', function (req, res) { // ... }); server.listen(8000); Declare GET route HTTP endpoints with Node.js -
app .post('/message', function (req, res) { // ... }) .get('/message',
function (req, res) { return r.table('messages') .orderBy({ index: 'created'}) .coerceTo('array') .run(r.conn) .then(function (messages) { res.json(messages); }); }) Get `messages` table HTTP endpoints with Node.js -
app .post('/message', function (req, res) { // ... }) .get('/message',
function (req, res) { return r.table('messages') .orderBy({ index: 'created'}) .coerceTo('array') .run(r.conn) .then(function (messages) { res.json(messages); }); }) Order by time created HTTP endpoints with Node.js -
app .post('/message', function (req, res) { // ... }) .get('/message',
function (req, res) { return r.table('messages') .orderBy({ index: 'created'}) .coerceTo('array') .run(r.conn) .then(function (messages) { res.json(messages); }); }) Run the query HTTP endpoints with Node.js -
app .post('/message', function (req, res) { // ... }) .get('/message',
function (req, res) { return r.table('messages') .orderBy({ index: 'created'}) .coerceTo('array') .run(r.conn) .then(function (messages) { res.json(messages); }); }) Return the results of the query HTTP endpoints with Node.js -
What we need • Add http endpoints for getting and
adding messages • Create an Angular factory for messages • Setup changefeed in Node.js app • Add realtime listener in factory -
Message Factory (Client) angular.module('rethinkDBWorkshop.services', []) .factory('MessageFactory', MessageFactory); MessageFactory.$inject = ['$http',
'$rootScope']; function MessageFactory ($http, $rootScope) { var messageCollection = []; var factory = { insertMessage: insertMessage, getMessageCollection: getMessageCollection, }; factory.getMessageCollection(); return factory; } Creating factory for messages -
Message Factory (Client) angular.module('rethinkDBWorkshop.services', []) .factory('MessageFactory', MessageFactory); MessageFactory.$inject = ['$http',
'$rootScope']; function MessageFactory ($http, $rootScope) { var messageCollection = []; var factory = { insertMessage: insertMessage, getMessageCollection: getMessageCollection, }; factory.getMessageCollection(); return factory; } Declare factory -
Message Factory (Client) angular.module('rethinkDBWorkshop.services', []) .factory('MessageFactory', MessageFactory); MessageFactory.$inject = ['$http',
'$rootScope']; function MessageFactory ($http, $rootScope) { var messageCollection = []; var factory = { insertMessage: insertMessage, getMessageCollection: getMessageCollection, }; factory.getMessageCollection(); return factory; } Inject our dependencies -
Message Factory (Client) angular.module('rethinkDBWorkshop.services', []) .factory('MessageFactory', MessageFactory); MessageFactory.$inject = ['$http',
'$rootScope']; function MessageFactory ($http, $rootScope) { var messageCollection = []; var factory = { insertMessage: insertMessage, getMessageCollection: getMessageCollection, }; factory.getMessageCollection(); return factory; } Declare our messages array -
Message Factory (Client) angular.module('rethinkDBWorkshop.services', []) .factory('MessageFactory', MessageFactory); MessageFactory.$inject = ['$http',
'$rootScope']; function MessageFactory ($http, $rootScope) { var messageCollection = []; var factory = { insertMessage: insertMessage, getMessageCollection: getMessageCollection, }; factory.getMessageCollection(); return factory; } Add method for adding messages -
Message Factory (Client) angular.module('rethinkDBWorkshop.services', []) .factory('MessageFactory', MessageFactory); MessageFactory.$inject = ['$http',
'$rootScope']; function MessageFactory ($http, $rootScope) { var messageCollection = []; var factory = { insertMessage: insertMessage, getMessageCollection: getMessageCollection, }; factory.getMessageCollection(); return factory; } Add method for getting messages -
Message Factory (Client) var factory = { insertMessage: function (text)
{ return $http.post('/message', { text: text, email: config.email }); }, getMessageCollection: function () {} }; Send POST http request -
Message Factory (Client) var factory = { insertMessage: function (text)
{ return $http.post('/message', { text: text, email: config.email }); }, getMessageCollection: function () {} }; Send object to be inserted -
Message Factory (Client) var factory = { insertMessage: function (text)
{ }, getMessageCollection: function () { return $http.get('/message') .then(function (res) { messageCollection.splice(0, Infinity); res.data.forEach(function (row) { messageCollection.push(row); }); return messageCollection; }); } }; GET messages from server -
Message Factory (Client) var factory = { insertMessage: function (text)
{ }, getMessageCollection: function () { return $http.get('/message') .then(function (res) { messageCollection.splice(0, Infinity); res.data.forEach(function (row) { messageCollection.push(row); }); return messageCollection; }); } }; Remove all messages from array -
Message Factory (Client) var factory = { insertMessage: function (text)
{ }, getMessageCollection: function () { return $http.get('/message') .then(function (res) { messageCollection.splice(0, Infinity); res.data.forEach(function (row) { messageCollection.push(row); }); return messageCollection; }); } }; Add all messages to array -
What we need • Add http endpoint for getting and
adding messages • Create an Angular factory for messages • Setup changefeed in Node.js app • Add realtime listener in factory -
Node.js Server var io = require('socket.io')(server); io.on('connection', function (socket) {
r.connect(config.get('rethinkdb')) .then(function (conn) { r.table('messages') .changes().run(conn) .then(function (cursor) { cursor.each(function (err, row) { socket.emit('message', row.new_val); }); }); }); }); Setup socket handler -
Node.js Server var io = require('socket.io')(server); io.on('connection', function (socket) {
r.connect(config.get('rethinkdb')) .then(function (conn) { r.table('messages') .changes().run(conn) .then(function (cursor) { cursor.each(function (err, row) { socket.emit('message', row.new_val); }); }); }); }); Create Socket.io instance -
Node.js Server var io = require('socket.io')(server); io.on('connection', function (socket) {
r.connect(config.get('rethinkdb')) .then(function (conn) { r.table('messages') .changes().run(conn) .then(function (cursor) { cursor.each(function (err, row) { socket.emit('message', row.new_val); }); }); }); }); Listen for new socket connections -
Node.js Server Create a new database connection var io =
require('socket.io')(server); io.on('connection', function (socket) { r.connect(config.get('rethinkdb')) .then(function (conn) { r.table('messages') .changes().run(conn) .then(function (cursor) { cursor.each(function (err, row) { socket.emit('message', row.new_val); }); }); }); }); -
Node.js Server var io = require('socket.io')(server); io.on('connection', function (socket) {
r.connect(config.get('rethinkdb')) .then(function (conn) { r.table('messages') .changes().run(conn) .then(function (cursor) { cursor.each(function (err, row) { socket.emit('message', row.new_val); }); }); }); }); Get `messages` table -
Node.js Server var io = require('socket.io')(server); io.on('connection', function (socket) {
r.connect(config.get('rethinkdb')) .then(function (conn) { r.table('messages') .changes().run(conn) .then(function (cursor) { cursor.each(function (err, row) { socket.emit('message', row.new_val); }); }); }); }); Listen to changes in our messages -
Node.js Server var io = require('socket.io')(server); io.on('connection', function (socket) {
r.connect(config.get('rethinkdb')) .then(function (conn) { r.table('messages') .changes().run(conn) .then(function (cursor) { cursor.each(function (err, row) { socket.emit('message', row.new_val); }); }); }); }); Run the query -
Node.js Server var io = require('socket.io')(server); io.on('connection', function (socket) {
r.connect(config.get('rethinkdb')) .then(function (conn) { r.table('messages') .changes().run(conn) .then(function (cursor) { cursor.each(function (err, row) { socket.emit('message', row.new_val); }); }); }); }); Pass a callback to our cursor -
Node.js Server var io = require('socket.io')(server); io.on('connection', function (socket) {
r.connect(config.get('rethinkdb')) .then(function (conn) { r.table('messages') .changes().run(conn) .then(function (cursor) { cursor.each(function (err, row) { socket.emit('message', row.new_val); }); }); }); }); Send new message to the client -
What we need • Add http endpoints for getting and
adding messages • Create an Angular factory for messages • Setup changefeed in Node.js app • Add realtime listener in factory -
Message Factory with Changefeeds function MessageFactory ($http, $rootScope) { var
socket = io.connect('http://' + config.url); var messageCollection = []; socket.on('message', function (message) { $rootScope.$apply(function () { messageCollection.push(message); }); }); var factory = { ... }; return factory; } Add new message handler -
Message Factory with Changefeeds function MessageFactory ($http, $rootScope) { var
socket = io.connect('http://' + config.url); var messageCollection = []; socket.on('message', function (message) { $rootScope.$apply(function () { messageCollection.push(message); }); }); var factory = { ... }; return factory; } Connection to Socket.io server -
Message Factory with Changefeeds function MessageFactory ($http, $rootScope) { var
socket = io.connect('http://' + config.url); var messageCollection = []; socket.on('message', function (message) { $rootScope.$apply(function () { messageCollection.push(message); }); }); var factory = { ... }; return factory; } Listen to new messages on socket -
Message Factory with Changefeeds function MessageFactory ($http, $rootScope) { var
socket = io.connect('http://' + config.url); var messageCollection = []; socket.on('message', function (message) { $rootScope.$apply(function () { messageCollection.push(message); }); }); var factory = { ... }; return factory; } Perform proper scope life cycle -
Message Factory with Changefeeds function MessageFactory ($http, $rootScope) { var
socket = io.connect('http://' + config.url); var messageCollection = []; socket.on('message', function (message) { $rootScope.$apply(function () { messageCollection.push(message); }); }); var factory = { ... }; return factory; } Push message into collection -
Additional Resources • RethinkDB website: http://rethinkdb.com • RethinkDB cookbook: http://rethinkdb.com/docs/cookbook
• RethinkDB installation: http://rethinkdb.com/docs/install/