Skip to content

Instantly share code, notes, and snippets.

@webdev
Created September 19, 2013 16:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webdev/6626171 to your computer and use it in GitHub Desktop.
Save webdev/6626171 to your computer and use it in GitHub Desktop.
require 'bundler'
Bundler.require
class Stream < Goliath::API
use Goliath::Rack::Params
use Goliath::Rack::Render, 'json'
use Goliath::Rack::Heartbeat
def response(env)
env.logger.info "Stream connection opened"
redis = Redis.new :driver => :synchrony
#EM.add_periodic_timer(1) { env.stream_send("data:hello ##{rand(100)}\n\n") }
EM.add_periodic_timer(1) { env.stream_send("\n") }
EM.run do
redis.psubscribe("syncs.xyz") do |on|
on.psubscribe do |channel, subscriptions|
puts "Subscribed to ##{channel} (#{subscriptions} subscriptions)"
env.stream_send('test')
end
on.pmessage do |pattern, channel, message|
puts "#{channel}: #{message}"
env.stream_send("#{channel}: #{message}")
end
on.punsubscribe do |channel, subscriptions|
puts "UNsubscribed to ##{channel} (#{subscriptions} subscriptions)"
end
end
end
streaming_response(202, {'X-Stream' => 'Goliath'})
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment