Created
September 19, 2013 16:41
-
-
Save webdev/6626171 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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