Using RethinkDB with Tornado & EventMachine

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.

Ba70f10866cbab0bc6b9b1d547ef8015?s=128

Ryan Paul

March 30, 2015
Tweet

Transcript

  1. 4.

    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
  2. 5.

    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
  3. 8.

    Changefeeds • The changes command returns a cursor that receives

    updates • Each update includes the new and old value of the modified record
  4. 9.
  5. 14.

    Async Web Frameworks • New in RethinkDB 2.0 Python &

    Ruby drivers • Download the release candidate today • Official release coming in April
  6. 15.
  7. 16.

    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)
  8. 17.

    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
  9. 18.

    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
  10. 19.

    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
  11. 20.

    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
  12. 21.

    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
  13. 22.

    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
  14. 23.

    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
  15. 24.

    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
  16. 25.

    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
  17. 26.

    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
  18. 27.

    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
  19. 28.

    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
  20. 29.

    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
  21. 31.

    Faye Framework • Open source pub/sub framework for realtime messaging

    • Communication between frontend and backend • Supports Ruby and Node.js on the backend
  22. 32.

    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
  23. 33.

    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
  24. 34.

    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
  25. 35.

    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
  26. 36.

    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
  27. 38.