Ruby on rails ActionController::Rails中的Live-SSE

Ruby on rails ActionController::Rails中的Live-SSE,ruby-on-rails,multithreading,concurrency,server-sent-events,puma,Ruby On Rails,Multithreading,Concurrency,Server Sent Events,Puma,我希望数据库中的更改能够反映在页面上,而无需重新加载服务器 控制器 class ProductController < ApplicationController include ActionController::Live def index @product = Product.available response.headers['Content-Type'] = 'text/event-stream' sse = SSE.new(response.

我希望数据库中的更改能够反映在页面上,而无需重新加载服务器

控制器

class ProductController < ApplicationController
  include ActionController::Live

  def index
    @product = Product.available
    response.headers['Content-Type'] = 'text/event-stream'
    sse = SSE.new(response.stream)
    sse.write @product
  ensure
    sse.close
  end
end 
class-ProductController
查看

<p><%= @product[:price] %></p>

我用的是美洲狮

当我在数据库中更新产品时,更改不会反映在网页上


我缺少什么?

Rails无法实时更新视图。它提供html,然后由一些JavaScript来监听流并处理事件

我创造了一个宝石,淋浴器,为你处理所有这些

使用淋浴,解决方案是这样的

首先,您需要发布更新事件,这可以通过产品模型上的after_update回调来完成

class Product < ActiveRecord::Base
    after_update :publish_event

    def publish_event
        Shower::Stream.publish('product.update', self)
    end
end

Redis当前不在我的堆栈中。是否有任何特殊的配置,我必须把这个工作到位@kPheasey您只需要在本地系统上安装redis。通过mac电脑上的自制软件或linux系统上的软件包安装起来非常容易,就像是淋浴器创建了一条路径——获取“/stream”到“淋浴/stream#index”——流是否流到/stream?如果是这样,Rails在访问页面时会抱怨:“无法修改冻结哈希”@KPheasey
$ ->
    stream = new Shower('/stream', ['product.update'])

    stream.addEventListener('product.update', (event) ->
      product = JSON.parse(event.data)
      $('p').html(product.price)
    )