在 Web 应用程序中使用 Rethinkdb changefeed
Using Rethinkdb's change feed in a webapp
如何在 Web 应用程序中使用 Rethinkdb 的更改提要,请参阅 http://www.rethinkdb.com/docs/changefeeds/ruby/?我目前在 Rails 上使用 Ruby。我试过谷歌搜索 'rethinkdb "change feed" rails' 和 'rethinkdb "change feed" websocket'
我想在网页上以尽可能低的延迟显示 RethinkDB table 的更新。
RethinkDB 似乎不支持复杂的客户端身份验证(身份验证令牌在所有客户端之间共享),因此您无法从 Javascript.
进行客户端验证
但是您可以在您的服务器上创建一个管道:运行 websocket,它将从 RethinkDB 中获取记录并将其传递给客户端。使用 em-websocket 它看起来像这样:
require 'em-websocket'
require 'rethinkdb'
include RethinkDB::Shortcuts
EventMachine.run do
@clients = []
@cursor = r.table("authors").changes.run
EM::WebSocket.start(:host => '0.0.0.0', :port => '3001') do |ws|
ws.onopen do |handshake|
@clients << ws
end
ws.onclose do
@clients.delete ws
end
@cursor.each do |document|
@clients.each{|ws| ws.send document}
end
end
end
RethinkDB 旨在从服务器(来自 Rails)而不是从客户端使用。了解这一点真的很重要!如果您的数据有一个侦听器(更改源),那么软管更改将被路由到您的 Rails 应用程序。
如果你想添加从前端(从浏览器)查询RethinkDB,你可能对这两个项目感兴趣:
https://github.com/mikemintz/rethinkdb-websocket-client
https://github.com/mikemintz/rethinkdb-websocket-server
将这些更改发送到您的应用程序后,您就可以随心所欲地使用它们了。如果您想做的是将这些更改路由到前端以仅向用户显示这些更改,您可以通过网络套接字发送它们。 Faye 是一个非常好的图书馆来做到这一点。
这就是它的样子。在您的 ruby 代码中,您可以添加如下内容:
# Add Faye
App = Faye::RackAdapter.new MessageApp, mount: "/faye"
# Changefeed listener
r.table("messages").changes.em_run(Conn) do |err, change|
App.get_client.publish('/message/new', change["new_val"])
end
基本上,每当 messages
table 发生变化时,都会通过网络套接字发送新值。您可以在此处查看完整示例(带有前端代码):
https://github.com/thejsj/ruby-and-rethinkdb/
这里是 Ruby 文件:
https://github.com/thejsj/ruby-and-rethinkdb/blob/master/server/main.rb
如何在 Web 应用程序中使用 Rethinkdb 的更改提要,请参阅 http://www.rethinkdb.com/docs/changefeeds/ruby/?我目前在 Rails 上使用 Ruby。我试过谷歌搜索 'rethinkdb "change feed" rails' 和 'rethinkdb "change feed" websocket'
我想在网页上以尽可能低的延迟显示 RethinkDB table 的更新。
RethinkDB 似乎不支持复杂的客户端身份验证(身份验证令牌在所有客户端之间共享),因此您无法从 Javascript.
进行客户端验证但是您可以在您的服务器上创建一个管道:运行 websocket,它将从 RethinkDB 中获取记录并将其传递给客户端。使用 em-websocket 它看起来像这样:
require 'em-websocket'
require 'rethinkdb'
include RethinkDB::Shortcuts
EventMachine.run do
@clients = []
@cursor = r.table("authors").changes.run
EM::WebSocket.start(:host => '0.0.0.0', :port => '3001') do |ws|
ws.onopen do |handshake|
@clients << ws
end
ws.onclose do
@clients.delete ws
end
@cursor.each do |document|
@clients.each{|ws| ws.send document}
end
end
end
RethinkDB 旨在从服务器(来自 Rails)而不是从客户端使用。了解这一点真的很重要!如果您的数据有一个侦听器(更改源),那么软管更改将被路由到您的 Rails 应用程序。
如果你想添加从前端(从浏览器)查询RethinkDB,你可能对这两个项目感兴趣:
https://github.com/mikemintz/rethinkdb-websocket-client
https://github.com/mikemintz/rethinkdb-websocket-server
将这些更改发送到您的应用程序后,您就可以随心所欲地使用它们了。如果您想做的是将这些更改路由到前端以仅向用户显示这些更改,您可以通过网络套接字发送它们。 Faye 是一个非常好的图书馆来做到这一点。
这就是它的样子。在您的 ruby 代码中,您可以添加如下内容:
# Add Faye
App = Faye::RackAdapter.new MessageApp, mount: "/faye"
# Changefeed listener
r.table("messages").changes.em_run(Conn) do |err, change|
App.get_client.publish('/message/new', change["new_val"])
end
基本上,每当 messages
table 发生变化时,都会通过网络套接字发送新值。您可以在此处查看完整示例(带有前端代码):
https://github.com/thejsj/ruby-and-rethinkdb/
这里是 Ruby 文件:
https://github.com/thejsj/ruby-and-rethinkdb/blob/master/server/main.rb