Class: Dashing::EventsController

Inherits:
ApplicationController show all
Includes:
ActionController::Live
Defined in:
app/controllers/dashing/events_controller.rb

Instance Method Summary collapse

Instance Method Details

#indexObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/controllers/dashing/events_controller.rb', line 5

def index
  @redis = Dashing.redis

  response.headers['Content-Type']      = 'text/event-stream'
  response.headers['X-Accel-Buffering'] = 'no'
  response.stream.write latest_events

  @redis.with do |redis_connection|
    redis_connection.psubscribe("#{Dashing.config.redis_namespace}.*") do |on|
      on.pmessage do |pattern, event, data|
        response.stream.write("data: #{data}\n\n")
      end
    end
  end
rescue IOError
  logger.info "[Dashing][#{Time.now.utc.to_s}] Stream closed"
ensure
  @redis.shutdown { |redis_connection| redis_connection.quit }
  response.stream.close
end

#latest_eventsObject



26
27
28
29
30
31
# File 'app/controllers/dashing/events_controller.rb', line 26

def latest_events
  @redis.with do |redis_connection|
    events = redis_connection.hvals("#{Dashing.config.redis_namespace}.latest")
    events.map { |v| "data: #{v}\n\n" }.join
  end
end