Created
November 10, 2017 09:10
-
-
Save scalaview/0ef7d685de4daaff7f036aba17d48389 to your computer and use it in GitHub Desktop.
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 "redis" | |
puts <<-EOS | |
To play with this example use redis-cli from another terminal, like this: | |
$ redis-cli publish one hello | |
Finally force the example to exit sending the 'exit' message with: | |
$ redis-cli publish two exit | |
EOS | |
redis = Redis.new | |
trap(:INT) { puts; exit } | |
begin | |
redis.subscribe(:one, :two) do |on| | |
on.subscribe do |channel, subscriptions| | |
puts "Subscribed to ##{channel} (#{subscriptions} subscriptions)" | |
end | |
on.message do |channel, message| | |
puts "##{channel}: #{message}" | |
redis.unsubscribe if message == "exit" | |
end | |
on.unsubscribe do |channel, subscriptions| | |
puts "Unsubscribed from ##{channel} (#{subscriptions} subscriptions)" | |
end | |
end | |
rescue Redis::BaseConnectionError => error | |
puts "#{error}, retrying in 1s" | |
sleep 1 | |
retry | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment