在Rails应用程序中,我使用Faye(机架适配器)进行推送通知(用于聊天).
我想将Faye用于另一个用例(更多推送通知),但我似乎无法弄明白.
在我的应用程序中,可以从后台作业创建模型,因此我想在创建模型时刷新我的一个视图(比如索引操作).
像这样:
应用程序/模型/ post.rb
class Post
include Mongoid::Document
after_create :notify_subscribers
private
def notify_subscribers
Faye::Client.publish("/posts")
end
end
Run Code Online (Sandbox Code Playgroud)
应用程序/视图/职位/ index.html.erb
<%= subscribe_to(posts_url) do %>
uhh what do I do here? ajax call to refresh the whole page??
<% end %>
Run Code Online (Sandbox Code Playgroud)
那么直接从after_create回调发布通知是一个好主意,当我从Faye服务器收到消息时,如何实现"更新"?我是否只是进行AJAX调用以从服务器重新加载数据?这似乎很慢.
此外,我想使用类似的东西来更新模型(比如说用户添加了一些评论或者作者改变了内容)所以一直颠覆数据库似乎不是一个好的计划......