Skip to content

Instantly share code, notes, and snippets.

@hakanensari
Created June 12, 2011 19:30
Show Gist options
  • Save hakanensari/1021906 to your computer and use it in GitHub Desktop.
Save hakanensari/1021906 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