Class: Shower::StreamController

Inherits:
ApplicationController
  • Object
show all
Includes:
ActionController::Live
Defined in:
app/controllers/shower/stream_controller.rb

Instance Method Summary collapse

Instance Method Details

#indexObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/controllers/shower/stream_controller.rb', line 6

def index
  response.headers['Content-Type'] = 'text/event-stream'
  redis = Redis.new

  redis.subscribe(params[:events].split(',') << 'heartbeat') do |on|
    on.message do |event, data|
      response.stream.write("event: #{event}\n")
      response.stream.write("data: #{data}\n\n")
    end
  end
rescue IOError
  # stream closed
ensure
  # stopping stream thread
  redis.quit
  response.stream.close
end