Skip to content

Instantly share code, notes, and snippets.

@pirj
Created July 31, 2012 13:49
Show Gist options
  • Save pirj/3217183 to your computer and use it in GitHub Desktop.
Save pirj/3217183 to your computer and use it in GitHub Desktop.
Sinatra stream + Redis PubSub
require 'bundler'
Bundler.require
class App < Sinatra::Base
# use Rack::FiberPool
get '/' do
# EM.run do
# EM.synchrony do
# redis = Redis.new(:host => "127.0.0.1", :port => 6379, :driver => :synchrony)
# puts "connected"
# puts redis.get("a")
# puts "1connected"
# EM.stop
# end
# end
stream do |out|
EM.synchrony do
redis = Redis.new :driver => :synchrony
puts "connected"
puts redis.get("a")
redis.psubscribe('*') do |on|
on.psubscribe do |channel, subscriptions|
puts "Subscribed to ##{channel} (#{subscriptions} subscriptions)"
end
on.pmessage do |pattern, channel, message|
puts "#{channel}: #{message}"
out << "#{channel}: #{message}"
end
on.punsubscribe do |channel, subscriptions|
puts "UNsubscribed to ##{channel} (#{subscriptions} subscriptions)"
end
end
end
end
end
end
source 'https://rubygems.org'
gem 'sinatra'
gem 'rack-fiber_pool', :require => 'rack/fiber_pool'
group :development do
gem 'thin'
gem 'pry-rails'
end
gem "redis", "~> 3.0"
gem "hiredis", "~> 0.4.5"
gem "redis-namespace"
gem "em-synchrony"
[pirj@tp pubsub]$ thin start
>> Using rack adapter
>> Thin web server (v1.4.1 codename Chromeo)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:3000, CTRL+C to stop
connected
/home/pirj/.rvm/gems/ruby-1.9.3-p0/gems/redis-3.0.1/lib/redis/connection/synchrony.rb:73:in `resume': fiber called across threads (FiberError)
from /home/pirj/.rvm/gems/ruby-1.9.3-p0/gems/redis-3.0.1/lib/redis/connection/synchrony.rb:73:in `block in connect'
from /home/pirj/.rvm/gems/ruby-1.9.3-p0/gems/eventmachine-1.0.0.beta.4/lib/em/deferrable.rb:151:in `call'
from /home/pirj/.rvm/gems/ruby-1.9.3-p0/gems/eventmachine-1.0.0.beta.4/lib/em/deferrable.rb:151:in `set_deferred_status'
from /home/pirj/.rvm/gems/ruby-1.9.3-p0/gems/eventmachine-1.0.0.beta.4/lib/em/deferrable.rb:191:in `succeed'
from /home/pirj/.rvm/gems/ruby-1.9.3-p0/gems/redis-3.0.1/lib/redis/connection/synchrony.rb:20:in `connection_completed'
from /home/pirj/.rvm/gems/ruby-1.9.3-p0/gems/eventmachine-1.0.0.beta.4/lib/eventmachine.rb:179:in `run_machine'
from /home/pirj/.rvm/gems/ruby-1.9.3-p0/gems/eventmachine-1.0.0.beta.4/lib/eventmachine.rb:179:in `run'
from /home/pirj/.rvm/gems/ruby-1.9.3-p0/gems/thin-1.4.1/lib/thin/backends/base.rb:63:in `start'
from /home/pirj/.rvm/gems/ruby-1.9.3-p0/gems/thin-1.4.1/lib/thin/server.rb:159:in `start'
from /home/pirj/.rvm/gems/ruby-1.9.3-p0/gems/thin-1.4.1/lib/thin/controllers/controller.rb:86:in `start'
from /home/pirj/.rvm/gems/ruby-1.9.3-p0/gems/thin-1.4.1/lib/thin/runner.rb:185:in `run_command'
from /home/pirj/.rvm/gems/ruby-1.9.3-p0/gems/thin-1.4.1/lib/thin/runner.rb:151:in `run!'
from /home/pirj/.rvm/gems/ruby-1.9.3-p0/gems/thin-1.4.1/bin/thin:6:in `<top (required)>'
from /home/pirj/.rvm/gems/ruby-1.9.3-p0/bin/thin:19:in `load'
from /home/pirj/.rvm/gems/ruby-1.9.3-p0/bin/thin:19:in `<main>'
[pirj@tp pubsub]$ thin start
>> Using rack adapter
>> Thin web server (v1.4.1 codename Chromeo)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:3000, CTRL+C to stop
connected
/home/pirj/.rvm/gems/ruby-1.9.3-p0/gems/redis-3.0.1/lib/redis/connection/synchrony.rb:76:in `yield': can't yield from root fiber (FiberError)
from /home/pirj/.rvm/gems/ruby-1.9.3-p0/gems/redis-3.0.1/lib/redis/connection/synchrony.rb:76:in `connect'
from /home/pirj/.rvm/gems/ruby-1.9.3-p0/gems/redis-3.0.1/lib/redis/client.rb:257:in `establish_connection'
from /home/pirj/.rvm/gems/ruby-1.9.3-p0/gems/redis-3.0.1/lib/redis/client.rb:63:in `connect'
from /home/pirj/.rvm/gems/ruby-1.9.3-p0/gems/redis-3.0.1/lib/redis/client.rb:276:in `ensure_connected'
from /home/pirj/.rvm/gems/ruby-1.9.3-p0/gems/redis-3.0.1/lib/redis/client.rb:167:in `block in process'
from /home/pirj/.rvm/gems/ruby-1.9.3-p0/gems/redis-3.0.1/lib/redis/client.rb:242:in `logging'
from /home/pirj/.rvm/gems/ruby-1.9.3-p0/gems/redis-3.0.1/lib/redis/client.rb:166:in `process'
from /home/pirj/.rvm/gems/ruby-1.9.3-p0/gems/redis-3.0.1/lib/redis/client.rb:78:in `call'
from /home/pirj/.rvm/gems/ruby-1.9.3-p0/gems/redis-3.0.1/lib/redis.rb:714:in `block in get'
from /home/pirj/.rvm/gems/ruby-1.9.3-p0/gems/redis-3.0.1/lib/redis.rb:36:in `block in synchronize'
from /home/pirj/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
from /home/pirj/.rvm/gems/ruby-1.9.3-p0/gems/redis-3.0.1/lib/redis.rb:36:in `synchronize'
from /home/pirj/.rvm/gems/ruby-1.9.3-p0/gems/redis-3.0.1/lib/redis.rb:713:in `get'
from /home/pirj/source/switchcoder/pubsub/app.rb:21:in `block (2 levels) in <class:App>'
from /home/pirj/.rvm/gems/ruby-1.9.3-p0/gems/sinatra-1.3.2/lib/sinatra/base.rb:296:in `block in stream'
from /home/pirj/.rvm/gems/ruby-1.9.3-p0/gems/sinatra-1.3.2/lib/sinatra/base.rb:264:in `call'
from /home/pirj/.rvm/gems/ruby-1.9.3-p0/gems/sinatra-1.3.2/lib/sinatra/base.rb:264:in `block in each'
from /home/pirj/.rvm/gems/ruby-1.9.3-p0/gems/eventmachine-1.0.0.beta.4/lib/eventmachine.rb:1012:in `call'
from /home/pirj/.rvm/gems/ruby-1.9.3-p0/gems/eventmachine-1.0.0.beta.4/lib/eventmachine.rb:1012:in `block in spawn_threadpool'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment