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.
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 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:
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 Publish the update to a channel