-
-
Save segphault/f9c8f4c769429fd8f65d to your computer and use it in GitHub Desktop.
Realtime web app built with Ruby and RethinkDB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "sinatra" | |
require "faye" | |
require "json" | |
require "rethinkdb" | |
include RethinkDB::Shortcuts | |
Faye::WebSocket.load_adapter "thin" | |
class Faye::Server | |
def scheme; end | |
end | |
EM.run do | |
Conn = r.connect host: "localhost", port: 28015 | |
class SinatraApp < Sinatra::Base | |
get "/" do | |
redirect "/index.html" | |
end | |
get "/todo" do | |
r.table("todo").coerce_to("array").run(Conn).to_json | |
end | |
end | |
App = Faye::RackAdapter.new SinatraApp, mount: "/faye" | |
r.table("todo").changes.em_run(Conn) do |err, change| | |
App.get_client.publish("/todo/update", change) | |
end | |
App.get_client.subscribe "/todo/add" do |m| | |
if m.is_a? String | |
r.table("todo").insert(text: m, done: false).run(Conn) | |
end | |
end | |
App.get_client.subscribe "/todo/done" do |m| | |
if !!m["done"] == m["done"] | |
r.table("todo").get(m["id"]).update(done: m["done"]).run(Conn) | |
end | |
end | |
Rack::Server.start app: App, Port: 4000 | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Realtime Ruby Example</title> | |
<script src="/faye/client.js"></script> | |
<script src="//www.polymer-project.org/0.5/components/webcomponentsjs/webcomponents.min.js"></script> | |
<link rel="import" href="//www.polymer-project.org/0.5/components/polymer/polymer.html"> | |
<link rel="import" href="//www.polymer-project.org/0.5/components/core-ajax/core-ajax.html"> | |
</head> | |
<body> | |
<template id="todo" is="auto-binding"> | |
<core-ajax auto url="/todo" handleAs="json" response="{{list}}"></core-ajax> | |
<template repeat="{{item in list}}"> | |
<label> | |
<input type="checkbox" on-change="{{check}}" checked="{{item.done}}"> | |
{{item.text}} | |
</label><br> | |
</template> | |
<input type="text" value="{{newItem}}" placeholder="New Item"> | |
<button on-click="{{add}}">Add</button> | |
</template> | |
<script> | |
var todo = document.querySelector("#todo"); | |
var faye = new Faye.Client('/faye'); | |
todo.add = function() { | |
faye.publish("/todo/add", todo.newItem); | |
}; | |
todo.check = function(ev, detail, sender) { | |
var item = ev.target.templateInstance.model.item; | |
faye.publish("/todo/done", { id: item.id, done: sender.checked }); | |
}; | |
faye.subscribe("/todo/update", function(data) { | |
if (data.new_val && !data.old_val) | |
return todo.list.push(data.new_val); | |
for (var item in todo.list) | |
if (todo.list[item].id === data.old_val.id) | |
return todo.list[item] = data.new_val; | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment