Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RethinkDB Introduction

RethinkDB Introduction

Avatar for Asad Dhamani

Asad Dhamani

November 06, 2015

Other Decks in Technology

Transcript

  1. What Is It • Document Database (JSON/Binary) • Realtime •

    Relational features • Easily scalable • Durable
  2. The Facts • Initial Release: July 2009 • Current Version:

    2.1 • Used widely in production • Open Source
  3. Who’s using it • Jiva Software & Mediafly - reactive

    web and mobile apps • pristine.io and Narrative Clip - cloud infrastructure for connected devices • Platzi and workshape.io - realtime analytics • CMUNE and NodeCraft - multiplayer games
  4. Installation brew update && brew install rethinkdb Mac: Linux(Ubuntu): source

    /etc/lsb-release echo "deb http://download.rethinkdb.com/apt $DISTRIB_CODENAME main" | sudo tee /etc/ apt/sources.list.d/rethinkdb.list wget -qO- http://download.rethinkdb.com/apt/pubkey.gpg | sudo apt-key add - sudo apt-get update sudo apt-get install rethinkdb Windows: :(
  5. Embeds into your language 1 import rethinkdb as r 2

    r.connect(host='localhost', db='test').repl() 3 r.table('messages').limit(1).run() 1 require "rethinkdb" 2 include RethinkDB::Shortcuts 3 r.connect(host: 'localhost', database: 'test').repl() 4 r.table('messages').limit(1).run() 1 r = require('rethinkdb'); 2 var connection = null; 3 r.connect( {host: 'localhost', database: 'test'}, function(err, conn) { 4 connection = conn; 5 }) 6 r.table('messages').limit(1).run(connection, function(err, result) { 7 })
  6. 1 import eventlet; eventlet.monkey_patch() 2 import rethinkdb as r 3

    from flask import Flask, jsonify 4 import threading 5 from flask_socketio import SocketIO 6 7 8 app = Flask(__name__) 9 app.config['SECRET_KEY'] = 'secret!' 10socketio = SocketIO(app) 11conn = r.connect(host='localhost', 12 port=28015, 13 db='chatrethink') main.py
  7. 1 def watch_db(): 2 conn = r.connect(host='localhost', 3 port=28015, 4

    db='chatrethink') 5 for x in r.table('messages').changes().run(conn): 6 data = {'text': x['new_val']['text'], 'username': x['new_val']['by']} 7 socketio.emit('broadcast event', data, namespace='/broadcast') 8 thread = threading.Thread(target=watch_db) 9 thread.daemon = True 10thread.start()
  8. 1 @app.route("/") 2 def hello(): 3 return app.send_static_file('index.html') 4 5

    6 @app.route('/_get_messages') 7 def get_messages(): 8 conn = r.connect(host='localhost', 9 port=28015, 10 db='chatrethink') 11 messages = r.table('messages').order_by(r.desc('added')).limit(25).run(conn) 12 messages = [{'text': message['text'], 'username': message['by']} for message in reversed(list(messages))] 13 return jsonify({'messages': messages})
  9. 1 @socketio.on('new message', namespace='/broadcast') 2 def handle_json(json): 3 data =

    {'text': json['text'], 'added': r.now(), 'by': json['username']} 4 r.table('messages').insert(data).run(conn) 5 6 7 if __name__ == "__main__": 8 socketio.run(app, debug=True)
  10. 1 <body ng-app="myApp"> 2 <link rel="stylesheet" href="/static/lib/foundation/css/foundation.css"> 3 <link rel="stylesheet"

    href="/static/css/styles.css"> 4 <script src="/static/lib/jquery/dist/jquery.js"></script> 5 <script type="text/javascript" src="/static/lib/socket.io-client/socket.io.js"></script> 6 <div class="grid" ng-controller="ChatController"> 7 <div class="row"> 8 <div class="small-6 columns"> 9 <div ng-repeat="message in messages"> 10 <span style="color: red;">{{message.username}}: </span>{{ message.text }} 11 </div> 12 </div> 13 </div> 14 <form ng-submit="newMessage()" ng-if="username !== undefined"> 15 <div class="row"> 16 <div class="small-6 columns"> 17 <input type="text" ng-model="message.text"> 18 </div> 19 <button class="button small">Send</button> 20 </div> 21 </form> /static/index.html
  11. 1 </form> 2 <form ng-submit="setUsername()" ng-if="username === undefined"> 3 <div

    class="row"> 4 <div class="small-6 columns"> 5 <input type="text" ng-model="user.name"> 6 </div> 7 <button class="button small">Set Username</button> 8 </div> 9 </form> 10</div> 11<script type="text/javascript" src="/static/lib/angularjs/angular.js"></script> 12<script type="text/javascript" src="/static/lib/angular-socket-io/socket.js"></ script> 13<script type="text/javascript" src="/static/lib/angular-cookies/angular- cookies.js"></script> 14<script type="text/javascript" src="/static/app.js"></script> 15</body>
  12. 1 var app = angular.module('myApp', [ 2 'btford.socket-io', 3 'ngCookies'

    4 ]); 5 6 app.factory('socket', function (socketFactory) { 7 var myIoSocket = io.connect('http://' + document.domain + ':' + location.port + '/broadcast'); 8 socket = socketFactory({ 9 ioSocket: myIoSocket 10 }); 11 return socket; 12}); /static/app.js
  13. 1 app.controller('ChatController', ['$scope', '$http', 'socket', '$cookies', function ($scope, $http, socket,

    $cookies) { 2 $scope.messages = []; 3 $scope.user = {name: ""}; 4 $scope.username = $cookies.get('rechat_username'); 5 socket.on('connect', function() { 6 $http({ 7 method: 'GET', 8 url: '/_get_messages' 9 }).then(function success(response) { 10 $scope.messages = response.data.messages; 11 }, function error(response) { 12 13 }); 14 socket.emit('my event', {data: 'I\'m connected!'}); 15 }); 16 $scope.message = {text: ""}; 17 18 $scope.newMessage = function () { 19 socket.emit('new message', {text: $scope.message.text, username: $scope.username}); 20 var messageObj = {text: $scope.message.text}; 21 $scope.message.text = ""; 22 }; 23 24 $scope.setUsername = function () { 25 $scope.username = $scope.user.name; 26 $cookies.put('rechat_username', $scope.username); 27 }; 28 29 socket.on('broadcast event', function(msg) { 30 msg = {text: msg.text, username: msg.username}; 31 if ($scope.messages.length >= 25) { 32 $scope.messages.shift(); 33 } 34 $scope.messages.push(msg); 35 }); 36 }]);
  14. Where it excels • Collaborative web and mobile apps •

    Streaming analytics apps • Multiplayer games • Realtime marketplaces • Connected devices