Created
September 14, 2011 15:05
-
-
Save mloughran/1216818 to your computer and use it in GitHub Desktop.
em-hiredis pubsub example
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 'em-hiredis' | |
EM.run { | |
require 'em-hiredis' | |
redis = EM::Hiredis.connect | |
# If you pass a block to subscribe it will be called whenever a message | |
# is received on this channel | |
redis.pubsub.subscribe('foo') { |msg| | |
p [:received_foo, msg] | |
} | |
# You can also pass any other object which responds to call if you wish | |
callback = Proc.new { |msg| | |
p [:received, msg] | |
} | |
redis.pubsub.subscribe('foo', callback) | |
# Passing such an object is useful if you want to unsubscribe | |
redis.pubsub.unsubscribe_proc('foo', callback) | |
# Or if you want to call a method on a certain object on message | |
class Thing | |
def receive_message(message) | |
puts "Foo received #{message}" | |
end | |
end | |
redis.pubsub.subscribe('foo', Thing.new.method(:receive_message)) | |
# You can also unsubscribe completely from a channel | |
# redis.pubsub.unsubscribe('foo') | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment