-
-
Save gerhard/1025208 to your computer and use it in GitHub Desktop.
non-blocking redis pub/sub in sinatra
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 "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