Skip to content

Commit

Permalink
Merge branch 'master' of github.com:ezmobius/redis-rb
Browse files Browse the repository at this point in the history
  • Loading branch information
soveran committed Oct 27, 2010
2 parents 81ecf98 + 65bac92 commit 554632b
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions examples/pubsub.rb
@@ -1,25 +1,31 @@
require 'rubygems'
require 'redis'

puts "To play with this example use redis-cli from another terminal, like this:"
puts " ./redis-cli publish one hello"
puts "Finally force the example to exit sending the 'exit' message with"
puts " ./redis-cli publish two exit"
puts ""

@redis = Redis.new(:timeout => 0)

@redis.subscribe('one','two') do |on|
on.subscribe {|klass, num_subs| puts "Subscribed to #{klass} (#{num_subs} subscriptions)" }
on.message do |klass, msg|
puts "#{klass} received: #{msg}"
if msg == 'exit'
@redis.unsubscribe
end
end
on.unsubscribe {|klass, num_subs| puts "Unsubscribed to #{klass} (#{num_subs} subscriptions)" }
end
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.connect

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 "##{klass}: #{message}"
redis.unsubscribe if message == "exit"
end

on.unsubscribe do |channel, subscriptions|
puts "Unsubscribed from ##{klass} (#{subscriptions} subscriptions)"
end
end

0 comments on commit 554632b

Please sign in to comment.