Share
Embed
Using RethinkDB with Tornado & EventMachine
This talk demonstrates how to use the Tornado and EventMachine integration in RethinkDB's Python and Ruby drivers. It also shows how to build realtime apps in Ruby with RethinkDB and Faye.
Ryan Paul
March 30, 2015
Transcript
38 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 -
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") .filter({name: "Bob"}).delete() Changefeed output: { new_val: null, 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: -
Async Web Frameworks • Event-driven programming • Non-blocking network I/O
• Well-suited for the realtime Web -
Async Web Frameworks • New in RethinkDB 2.0 Python &
Ruby drivers • Download the release candidate today • Official release coming in April -
Async Web Frameworks • Perform queries asynchronously • Consume multiple
changefeeds at the same time without blocking -
Tornado import rethinkdb as r from tornado import ioloop, gen
r.set_loop_type("tornado") @gen.coroutine def print_changes(): conn = yield r.connect(host="localhost", port=28015) feed = yield r.table("table").changes().run(conn) while (yield feed.fetch_next()) change = yield feed.next() print(change) ioloop.IOLoop.current().add_callback(print_changes) -
Tornado import rethinkdb as r from tornado import ioloop, gen
r.set_loop_type("tornado") @gen.coroutine def print_changes(): conn = yield r.connect(host="localhost", port=28015) feed = yield r.table("table").changes().run(conn) while (yield feed.fetch_next()) change = yield feed.next() print(change) ioloop.IOLoop.current().add_callback(print_changes) Configure the event loop -
Tornado import rethinkdb as r from tornado import ioloop, gen
r.set_loop_type("tornado") @gen.coroutine def print_changes(): conn = yield r.connect(host="localhost", port=28015) feed = yield r.table("table").changes().run(conn) while (yield feed.fetch_next()) change = yield feed.next() print(change) ioloop.IOLoop.current().add_callback(print_changes) Add the coroutine decorator -
Tornado import rethinkdb as r from tornado import ioloop, gen
r.set_loop_type("tornado") @gen.coroutine def print_changes(): conn = yield r.connect(host="localhost", port=28015) feed = yield r.table("table").changes().run(conn) while (yield feed.fetch_next()) change = yield feed.next() print(change) ioloop.IOLoop.current().add_callback(print_changes) Connect to the database -
Tornado import rethinkdb as r from tornado import ioloop, gen
r.set_loop_type("tornado") @gen.coroutine def print_changes(): conn = yield r.connect(host="localhost", port=28015) feed = yield r.table("table").changes().run(conn) while (yield feed.fetch_next()) change = yield feed.next() print(change) ioloop.IOLoop.current().add_callback(print_changes) Perform the ReQL query -
Tornado import rethinkdb as r from tornado import ioloop, gen
r.set_loop_type("tornado") @gen.coroutine def print_changes(): conn = yield r.connect(host="localhost", port=28015) feed = yield r.table("table").changes().run(conn) while (yield feed.fetch_next()) change = yield feed.next() print(change) ioloop.IOLoop.current().add_callback(print_changes) Iterate over changefeed changes -
Tornado import rethinkdb as r from tornado import ioloop, gen
r.set_loop_type("tornado") @gen.coroutine def print_changes(): conn = yield r.connect(host="localhost", port=28015) feed = yield r.table("table").changes().run(conn) while (yield feed.fetch_next()) change = yield feed.next() print(change) ioloop.IOLoop.current().add_callback(print_changes) Fetch the latest change -
Tornado import rethinkdb as r from tornado import ioloop, gen
r.set_loop_type("tornado") @gen.coroutine def print_changes(): conn = yield r.connect(host="localhost", port=28015) feed = yield r.table("table").changes().run(conn) while (yield feed.fetch_next()) change = yield feed.next() print(change) ioloop.IOLoop.current().add_callback(print_changes) Print the change to stdout -
Tornado import rethinkdb as r from tornado import ioloop, gen
r.set_loop_type("tornado") @gen.coroutine def print_changes(): conn = yield r.connect(host="localhost", port=28015) feed = yield r.table("table").changes().run(conn) while (yield feed.fetch_next()) change = yield feed.next() print(change) ioloop.IOLoop.current().add_callback(print_changes) Run coroutine in the background -
EventMachine require "rethinkdb" include RethinkDB::Shortcuts conn = r.connect host: "localhost",
port: 28015 EM.run do r.table("table").changes.em_run(conn) do |err, change| puts change end end -
EventMachine require "rethinkdb" include RethinkDB::Shortcuts conn = r.connect host: "localhost",
port: 28015 EM.run do r.table("table").changes.em_run(conn) do |err, change| puts change end end Connect to the database -
EventMachine require "rethinkdb" include RethinkDB::Shortcuts conn = r.connect host: "localhost",
port: 28015 EM.run do r.table("table").changes.em_run(conn) do |err, change| puts change end end Create an EventMachine block -
EventMachine require "rethinkdb" include RethinkDB::Shortcuts conn = r.connect host: "localhost",
port: 28015 EM.run do r.table("table").changes.em_run(conn) do |err, change| puts change end end Perform the ReQL query -
EventMachine require "rethinkdb" include RethinkDB::Shortcuts conn = r.connect host: "localhost",
port: 28015 EM.run do r.table("table").changes.em_run(conn) do |err, change| puts change end end Print the change to stdout -
Faye Framework • Open source pub/sub framework for realtime messaging
• Communication between frontend and backend • Supports Ruby and Node.js on the backend -
Faye Framework EM.run do ... App = Faye::RackAdapter.new Sinatra::Application, mount:
"/faye" conn = r.connect host: "localhost", port: 28015 r.table("todo").changes.em_run(conn) do |err, change| App.get_client.publish("/todo/update", change) end ... end Server-side Ruby code -
Faye Framework EM.run do ... App = Faye::RackAdapter.new Sinatra::Application, mount:
"/faye" conn = r.connect host: "localhost", port: 28015 r.table("todo").changes.em_run(conn) do |err, change| App.get_client.publish("/todo/update", change) end ... end Initialize Faye and bind URL route -
Faye Framework EM.run do ... App = Faye::RackAdapter.new Sinatra::Application, mount:
"/faye" conn = r.connect host: "localhost", port: 28015 r.table("todo").changes.em_run(conn) do |err, change| App.get_client.publish("/todo/update", change) end ... end Connect to the database -
Faye Framework EM.run do ... App = Faye::RackAdapter.new Sinatra::Application, mount:
"/faye" conn = r.connect host: "localhost", port: 28015 r.table("todo").changes.em_run(conn) do |err, change| App.get_client.publish("/todo/update", change) end ... end Perform the ReQL query -
Faye Framework EM.run do ... App = Faye::RackAdapter.new Sinatra::Application, mount:
"/faye" conn = r.connect host: "localhost", port: 28015 r.table("todo").changes.em_run(conn) do |err, change| App.get_client.publish("/todo/update", change) end ... end Publish the update to a channel