Rails pub/sub 与 faye

发布于 2024-11-28 21:28:04 字数 753 浏览 0 评论 0原文

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

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

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

像这样:

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

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

那么直接从 after_create 回调发布通知是一个好主意 当我从 Faye 服务器收到消息时,如何实施“更新”? 我是否只需执行 AJAX 调用即可从服务器重新加载数据?这样看来会很慢。

此外,我想使用类似的东西来更新模型(假设用户添加了一些评论或作者更改了内容),因此一直对数据库进行攻击似乎不是一个好的计划......

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

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.

Like so:

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

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

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彩扇题诗 2024-12-05 21:28:04

首先,是的,使用 after_create 发布通知就可以了。您应该在 notify_subscribers 方法中执行的操作是发布有关您要在客户端中使用的新帖子的所有相关信息,这样您就不必再做一次收到通知时不必要的 AJAX 请求。

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

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):

<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 并在 subscribe 回调函数中使用 jQuery 触发 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.

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

...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>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文