Skip to content

Instantly share code, notes, and snippets.

@penso
Created May 23, 2013 15:39
Show Gist options
  • Save penso/5637005 to your computer and use it in GitHub Desktop.
Save penso/5637005 to your computer and use it in GitHub Desktop.
# put this in lib/realtime_analytics.rb
#
# Then add in your config/routes.rb:
# mount AnalyticsSSE => '/realtime-analytics'
require 'em-hiredis'
class AnalyticsSSE < Sinatra::Base
include Sinatra::SSE
get '/' do
sse_stream do |out|
redis = EM::Hiredis.connect("redis://#{Settings[:redis_host]}:#{Settings[:redis_port]}")
redis.get(params[:hash]) do |value|
if ! value
out.push :data => "Not Authorized"
else
if params[:ids]
params[:ids].split(',').each do |val|
redis.pubsub.subscribe(val) do |msg|
out.push :event => val, :data => msg.to_s
end
end
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment