get "/" do; ...; end get '/messages' do; ...; end post '/messages' do; ...; end end Rack::Server.start app: MessageApp, Port: 8000 Create a Sinatra app
get "/" do; ...; end get '/messages' do; ...; end post '/messages' do; ...; end end Rack::Server.start app: MessageApp, Port: 8000 Route to static assets
get "/" do; ...; end get '/messages' do; ...; end post '/messages' do; ...; end end Rack::Server.start app: MessageApp, Port: 8000 Create a route to get messages
get "/" do; ...; end get '/messages' do; ...; end post '/messages' do; ...; end end Rack::Server.start app: MessageApp, Port: 8000 Create a route to post messages
get "/" do; ...; end get '/messages' do; ...; end post '/messages' do; ...; end end Rack::Server.start app: MessageApp, Port: 8000 Start app in port 8000
class MessageApp < Sinatra::Base ... end App = Faye::RackAdapter.new MessageApp, mount: "/faye" r.table("messages").changes.em_run(Conn) do |err, change| App.get_client.publish('/messages/new', change["new_val"]) end Rack::Server.start app: App, Port: 8000 end Connect to the database
class MessageApp < Sinatra::Base ... end App = Faye::RackAdapter.new MessageApp, mount: "/faye" r.table("messages").changes.em_run(Conn) do |err, change| App.get_client.publish('/messages/new', change["new_val"]) end Rack::Server.start app: App, Port: 8000 end Add your Sinatra app
class MessageApp < Sinatra::Base ... end App = Faye::RackAdapter.new MessageApp, mount: "/faye" r.table("messages").changes.em_run(Conn) do |err, change| App.get_client.publish('/messages/new', change["new_val"]) end Rack::Server.start app: App, Port: 8000 end Start the changefeed
class MessageApp < Sinatra::Base ... end App = Faye::RackAdapter.new MessageApp, mount: "/faye" r.table("messages").changes.em_run(Conn) do |err, change| App.get_client.publish('/messages/new', change["new_val"]) end Rack::Server.start app: App, Port: 8000 end Publish the update to a channel
class MessageApp < Sinatra::Base ... end App = Faye::RackAdapter.new MessageApp, mount: "/faye" r.table("messages").changes.em_run(Conn) do |err, change| App.get_client.publish('/messages/new', change["new_val"]) end Rack::Server.start app: App, Port: 8000 end Start Faye app on port 8000
to your app • By doing this, your database remains as your central source of truth • Having a database that listens to changes in your data dramatically simplifies building realtime apps