class SseController < ApplicationController include ActionController::Live def sse_chrome response.headers["Content-Type"] = "text/event-stream" redis_sub_chrome = Redis.new() subscribe_channel = 'listener_chrome' #Thread.new do redis_sub_chrome.subscribe(subscribe_channel) do |on| on.message do |event, data| response.stream.write("event: #{event}\n") response.stream.write("data: #{data}\n\n") end end #end rescue IOError logger.info "Stream Closed" ensure redis_sub_chrome.quit response.stream.close end # Fn to publish data with handle for the listener on chrome def publish_to_chrome response.headers["Content-Type"] = "text/event-stream" redis_chrome = Redis.new() 10.times{ redis_chrome.publish('listener_chrome', 'Data for Chrome!') } render nothing: true end def sse_ff response.headers["Content-Type"] = "text/event-stream" redis_sub_chrome = Redis.new() subscribe_channel = 'listener_ff' #Thread.new do redis_sub_chrome.subscribe(subscribe_channel) do |on| on.message do |event, data| response.stream.write("event: #{event}\n") response.stream.write("data: #{data}\n\n") end end #end rescue IOError logger.info "Stream Closed" ensure redis_sub_chrome.quit response.stream.close end # Fn to publish data with handle for the listener on FireFox def publish_to_ff response.headers["Content-Type"] = "text/event-stream" redis_chrome = Redis.new() 10.times{ redis_chrome.publish('listener_ff', 'Data for FF!') } render nothing: true end # Fn to show simple ACL demo def hello_world response.headers['Content-Type'] = 'text/event-stream' 10.times do |i| response.stream.write "hello world #{i}\n" sleep 1 end response.stream.close end end