Skip to content

Instantly share code, notes, and snippets.

@gerhard
Forked from hakanensari/app.rb
Created June 14, 2011 15:58
Show Gist options
  • Save gerhard/1025208 to your computer and use it in GitHub Desktop.
Save gerhard/1025208 to your computer and use it in GitHub Desktop.
non-blocking redis pub/sub in sinatra
require "fiber"
require "rack/fiber_pool"
require "sinatra/base"
require "redis"
require "redis/connection/synchrony"
class App < Sinatra::Base
use Rack::FiberPool
get "/:channel" do |channel|
redis = Redis.connect
redis.subscribe(channel) do |on|
on.message do |channel, message|
redis.unsubscribe
body "#{channel}: #{message}"
end
end
end
end
# rackup -s thin app.rb
#
# curl http://localhost:9292/foo
# curl http://localhost:9292/bar
#
# redis-cli publish foo "hello"
# redis-cli publish bar "hola"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment