Class: Meerkat::Backend::Redis
- Inherits:
-
Object
- Object
- Meerkat::Backend::Redis
- Defined in:
- lib/meerkat/backend/redis.rb
Instance Method Summary collapse
-
#initialize(redis_uri = nil) ⇒ Redis
constructor
A new instance of Redis.
- #publish(topic, json) ⇒ Object
- #subscribe(topic, &callback) ⇒ Object
- #unsubscribe(sub) ⇒ Object
Constructor Details
#initialize(redis_uri = nil) ⇒ Redis
Returns a new instance of Redis.
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/meerkat/backend/redis.rb', line 6 def initialize(redis_uri = nil) @subs = {} EM.next_tick do @sub = EM::Hiredis.connect redis_uri @pub = EM::Hiredis.connect redis_uri @sub.on :pmessage do |topic, channel, | @subs[topic].each { |cb| cb.call channel, } end end end |
Instance Method Details
#publish(topic, json) ⇒ Object
17 18 19 |
# File 'lib/meerkat/backend/redis.rb', line 17 def publish(topic, json) @pub.publish topic, json end |
#subscribe(topic, &callback) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/meerkat/backend/redis.rb', line 21 def subscribe(topic, &callback) if @subs[topic] @subs[topic] << callback else @subs[topic] = [ callback ] EM.next_tick do @sub.psubscribe topic end end [topic, callback] end |
#unsubscribe(sub) ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/meerkat/backend/redis.rb', line 33 def unsubscribe(sub) topic, cb = sub @subs[topic].delete cb if @subs[topic].empty? EM.next_tick do @subs.delete topic @sub.punsubscribe(topic) end end end |