Rails pub/sub 与 faye [英] Rails pub/sub with faye

查看:55
本文介绍了Rails pub/sub 与 faye的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Rails 应用中,我使用 Faye(机架适配器)来推送通知(用于聊天).

In a Rails app I've used Faye (Rack adapter) for push notifications (for chat).

我想将 Faye 用于另一个用例(更多推送通知),但我似乎无法弄清楚.

I want to use Faye for another use case (more push notifications) but I can't seem to figure it out.

在我的应用中,可以从后台作业创建模型,因此我想在创建模型时刷新我的一个视图(比如索引操作).

In my app a model can be created from a background job so I want to refresh one of my views (say the index action) when the model gets created.

像这样:

app/models/post.rb

app/models/post.rb

class Post
    include Mongoid::Document

    after_create :notify_subscribers

    private
    def notify_subscribers
        Faye::Client.publish("/posts")
    end
end

app/views/posts/index.html.erb

app/views/posts/index.html.erb

<%= subscribe_to(posts_url) do %>
   uhh what do I do here? ajax call to refresh the whole page??
<% end %>

所以直接从 after_create 回调中发布通知是个好主意当我从 Faye 服务器收到消息时,我该如何实施更新"?我是否只是通过 AJAX 调用从服务器重新加载数据?这似乎会很慢.

So is publishing the notification directly from an after_create callback a good idea and when I get a message from the Faye server how to I go about implementing the "update"? Do I just do an AJAX call to reload the data from the server? That seems like it would be slow.

此外,我想使用类似的东西来更新模型(比如用户添加了一些评论或作者更改了内容),所以一直在捣鼓数据库似乎不是一个好的计划......

Further I want to use something similar for updates to the model (say a user added some comments or the author changed the content) so thrashing the DB all the time doesn't seem like a good plan...

推荐答案

首先,是的,使用 after_create 发布通知很好.您应该在 notify_subscribers 方法中做的是发布有关您要在客户端中使用的新帖子的所有相关信息,以便您不必制作另一个收到通知时不必要的 AJAX 请求.

First of all, yes, publishing the notification with after_create is fine. What you should do in your notify_subscribers method is to publish all relevant information about the new post that you want to use in the client so that you don't have to make another unnecessary AJAX request when the notification is received.

因此,例如,如果帖子的 titlecontent 与用户在创建后立即看到的内容相关,您可以执行以下操作:

So, for instance, if the title and content of the post are relevant for the user to see immediately after it gets created, you would do something like:

def notify_subscribers
  client = Faye::Client.new('http://localhost:3000/faye')
  client.publish("/posts/new", {
    'title' => self.title,
    'content' => self.content
  })
end

...然后,在视图中,您可能会使用 jQuery 来使用有关您从服务器收到的新帖子的数据.Rails 代码在这里帮不了你.例如.你会这样做(假设你正在使用 jQuery 并且在你的头文件中包含了 faye.js):

...and then, in the view, you would probably use jQuery to use the data about the new post which you receive from the server. Rails code won't help you here. E.g. you would do this (assuming you're using jQuery and have included faye.js in your header):

<script type="text/javascript">
$(function() {
  var client = new Faye.Client('http://localhost:3000/faye');
  client.subscribe("/posts/new", function(data) {
    /* do whatever you need with data */
  });
});
</script>

最后,如果您想传输的信息太复杂而无法作为通知的一部分传输,或者您已经有更新视图的现有 AJAX 流程,您可以发布帖子的 ID 并使用 jQuery 触发 AJAX 调用在 subscribe 回调函数中.不过,我只会在必要时推荐这个.

Finally, if somehow the information you want to transfer is too complex to transfer as part of the notification, or you already have existing AJAX processes for updating your view, you could just publish the post's ID and trigger an AJAX call with jQuery in the subscribe callback function. I'd recommend this only if necessary though.

这篇关于Rails pub/sub 与 faye的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆