Skip to content

Instantly share code, notes, and snippets.

@mloughran
Created September 14, 2011 15:05

Revisions

  1. mloughran revised this gist Sep 14, 2011. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions pubsub_example.rb
    Original file line number Diff line number Diff line change
    @@ -19,6 +19,14 @@
    # 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')
    }
  2. Martyn Loughran created this gist Sep 14, 2011.
    24 changes: 24 additions & 0 deletions pubsub_example.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    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)

    # You can also unsubscribe completely from a channel
    # redis.pubsub.unsubscribe('foo')
    }