Sha256: 7440cedf9a68fd563bf79036503f04b64f2e06b4487a494ce568f1c363cadb9c

Contents?: true

Size: 693 Bytes

Versions: 3

Compression:

Stored size: 693 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 }

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
redis-3.0.1 examples/pubsub.rb
redis-3.0.0 examples/pubsub.rb
redis-3.0.0.rc2 examples/pubsub.rb