Created
August 17, 2012 10:13
-
-
Save pirj/3377714 to your computer and use it in GitHub Desktop.
Sinatra streaming + Redis PubSub
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 App < Sinatra::Base | |
helpers Sinatra::Streaming | |
get '/' do | |
subscriber = EM::Hiredis.connect | |
subscriber.psubscribe '*' | |
stream :keep_open do |out| | |
subscriber.on(:pmessage) do |key, channel, message| | |
subscriber.punsubscribe '*' if out.closed? | |
out << "#{key}, #{channel}, #{message}" unless out.closed? | |
end | |
end | |
end | |
end |
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
source 'https://rubygems.org' | |
gem 'sinatra' | |
gem 'sinatra-contrib', require: 'sinatra/streaming' | |
group :development do | |
gem 'thin' | |
gem 'pry-rails' | |
end | |
gem 'eventmachine', "= 1.0.0.rc.4" | |
gem "redis", "~> 3.0" | |
gem "hiredis", "~> 0.4.5" | |
gem 'em-hiredis' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example client code (JS)