Skip to content

Instantly share code, notes, and snippets.

@csquared
Created February 14, 2014 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csquared/9010479 to your computer and use it in GitHub Desktop.
Save csquared/9010479 to your computer and use it in GitHub Desktop.
require 'redis'
$redis = Redis.new
def pub
puts 'pub'
$redis.publish('one', 'two')
$redis.publish(:one, 'two')
#$redis.publish(:one, 'exit')
end
def sub
$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
end
t = Thread.new { sub }
Thread.new { pub }
t.join
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment