Sha256: e238ee5951666607bd5fe3e4e71b32852ed787864446d38674dfbb3ce3519729

Contents?: true

Size: 822 Bytes

Versions: 35

Compression:

Stored size: 822 Bytes

Contents

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

Version data entries

35 entries across 34 versions & 9 rubygems

Version Path
finsync_redis-3.3.5 examples/pubsub.rb
redis-4.0.3 examples/pubsub.rb
redis-4.1.0.beta1 examples/pubsub.rb
redis-4.0.2 examples/pubsub.rb
redis-3.3.5 examples/pubsub.rb
redis-4.0.1 examples/pubsub.rb
redis-4.0.0 examples/pubsub.rb
logstash-filter-cache-redis-0.3.1 vendor/bundle/jruby/1.9/gems/redis-3.3.3/examples/pubsub.rb
logstash-filter-cache-redis-0.3.0 vendor/bundle/jruby/1.9/gems/redis-3.3.3/examples/pubsub.rb
logstash-filter-cache-redis-0.2.0 vendor/bundle/jruby/1.9/gems/redis-3.3.3/examples/pubsub.rb
logstash-filter-cache-redis-0.1.0 vendor/bundle/jruby/1.9/gems/redis-3.3.3/examples/pubsub.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/redis-3.3.3/examples/pubsub.rb
redis-3.3.3 examples/pubsub.rb
abaci-0.3.0 vendor/bundle/gems/redis-3.3.2/examples/pubsub.rb
abaci-0.3.0 vendor/bundle/gems/redis-3.3.1/examples/pubsub.rb
redis-4.0.0.rc1 examples/pubsub.rb
redis-3.3.2 examples/pubsub.rb
redis-3.3.1 examples/pubsub.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/redis-3.3.0/examples/pubsub.rb
redis-3.3.0 examples/pubsub.rb