簡體   English   中英

使用ActionController進行流式處理:: Live無法在生產中使用

[英]Streaming with ActionController::Live not working in production

我正在使用Ruby on Rails 4.1,使用ActionController :: Live從Sidekiq進程傳輸數據。 在開發中,我的流媒體工作非常棒。 在生產中(使用Nginx / Puma),它並不是很好。 這是正在發生的事情。

在制作中,參考我的Firebug下面的圖像,“/ events”被多次發射。 為什么我的EventSource會被反復觸發而不是等待我的數據? 這在開發中不會發生。

Firebug EventSource

只要我的Sidekiq進程正在運行,它將以隨機間隔重復觸發。 一旦我的sidekiq進程完成,它將掛起並且不再啟動。 那最后一個最終會超時(見圖中的紅色文字)

這是我的coffeescript:

source = new EventSource('/events')
source.addEventListener 'my_event', (e) ->
    console.log 'Got a message!'
    # Add the content to the screen somewhere

參考Firebug圖像,它幾乎就像我在Sidekiq過程中超時一樣。 我從工人那里發帖到redis。

初始化

REDIS = Redis.new(url: 'redist://localhost:6379')

Sidekiq工人

REDIS.publish('my_event', 'some data')

然后我有連接EventSource的控制器動作:

def events
  response.headers["Content-Type"] = "text/event-stream"
  redis = Redis.new(url: "redist://localhost:6379")
  # blocks the current thread
  redis.subscribe(['my_event', 'heartbeat']) do |on|
    on.message do |event, data|
      if event == 'heartbeat'
        response.stream.write("event: heartbeat\ndata: heartbeat\n\n")
      elsif event == 'my_event'
        response.stream.write("event: #{event}\n")
        response.stream.write("data: #{data.to_json}\n\n")
      end
    end
  end
rescue IOError
  logger.info 'Events stream closed'
ensure
  logger.info 'Stopping events streaming thread'
  redis.quit
  response.stream.close
end

再次,這在開發中100%出色。 來自Sidekiq的數據完全流向瀏覽器。 上面的心跳是因為這個答案 (我有同樣的問題)

有人看到我做錯了嗎? 我是否缺少ActionController::Live某種形式的設置來使用Nginx / Puma在生產中工作?

重要的提示

在每個來自圖像的請求(它似乎超時並嘗試執行下一個請求)的非常結束時,我成功獲得了一行數據。 看起來似乎有些東西在起作用。 但是單個EventSource並沒有保持開放狀態並且在數據上聽得足夠長,它只是不斷重復。

我最近的一個項目遇到了類似的問題。 我發現這是我的nginx配置。

將以下內容添加到位置部分:

proxy_set_header Connection '';
proxy_http_version 1.1;
chunked_transfer_encoding off;
proxy_buffering off;
proxy_cache off;

此信息來自以下Stack Overflow討論:

通過Nginx發送EventSource / Server-Sent事件

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM