Sha256: 683cb85bc8b767870d6033c91dade75a995ca13e9394e60bb2dd9985f69fc468

Contents?: true

Size: 855 Bytes

Versions: 1

Compression:

Stored size: 855 Bytes

Contents

module MyDashboard
  class EventsController < ApplicationController
    include ActionController::Live

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

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
my_dashboard-0.8.6 app/controllers/my_dashboard/events_controller.rb