Skip to content

Instantly share code, notes, and snippets.

@mloughran
Created September 14, 2011 15:05
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mloughran/1216818 to your computer and use it in GitHub Desktop.
Save mloughran/1216818 to your computer and use it in GitHub Desktop.
em-hiredis pubsub example
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