Module: Redis::Commands::Pubsub

Included in:
Redis::Commands
Defined in:
lib/redis/commands/pubsub.rb

Instance Method Summary collapse

Instance Method Details

#psubscribe(*channels, &block) ⇒ Object

Listen for messages published to channels matching the given patterns.



40
41
42
43
44
# File 'lib/redis/commands/pubsub.rb', line 40

def psubscribe(*channels, &block)
  synchronize do |_client|
    _subscription(:psubscribe, 0, channels, block)
  end
end

#psubscribe_with_timeout(timeout, *channels, &block) ⇒ Object

Listen for messages published to channels matching the given patterns. Throw a timeout error if there is no messages for a timeout period.



48
49
50
51
52
# File 'lib/redis/commands/pubsub.rb', line 48

def psubscribe_with_timeout(timeout, *channels, &block)
  synchronize do |_client|
    _subscription(:psubscribe_with_timeout, timeout, channels, block)
  end
end

#publish(channel, message) ⇒ Object

Post a message to a channel.



7
8
9
# File 'lib/redis/commands/pubsub.rb', line 7

def publish(channel, message)
  send_command([:publish, channel, message])
end

#pubsub(subcommand, *args) ⇒ Object

Inspect the state of the Pub/Sub subsystem. Possible subcommands: channels, numsub, numpat.



65
66
67
# File 'lib/redis/commands/pubsub.rb', line 65

def pubsub(subcommand, *args)
  send_command([:pubsub, subcommand] + args)
end

#punsubscribe(*channels) ⇒ Object

Stop listening for messages posted to channels matching the given patterns.



55
56
57
58
59
60
61
# File 'lib/redis/commands/pubsub.rb', line 55

def punsubscribe(*channels)
  raise "Can't unsubscribe if not subscribed." unless subscribed?

  synchronize do |_client|
    _subscription(:punsubscribe, 0, channels, nil)
  end
end

#subscribe(*channels, &block) ⇒ Object

Listen for messages published to the given channels.



16
17
18
19
20
# File 'lib/redis/commands/pubsub.rb', line 16

def subscribe(*channels, &block)
  synchronize do |_client|
    _subscription(:subscribe, 0, channels, block)
  end
end

#subscribe_with_timeout(timeout, *channels, &block) ⇒ Object

Listen for messages published to the given channels. Throw a timeout error if there is no messages for a timeout period.



24
25
26
27
28
# File 'lib/redis/commands/pubsub.rb', line 24

def subscribe_with_timeout(timeout, *channels, &block)
  synchronize do |_client|
    _subscription(:subscribe_with_timeout, timeout, channels, block)
  end
end

#subscribed?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/redis/commands/pubsub.rb', line 11

def subscribed?
  !@subscription_client.nil?
end

#unsubscribe(*channels) ⇒ Object

Stop listening for messages posted to the given channels.



31
32
33
34
35
36
37
# File 'lib/redis/commands/pubsub.rb', line 31

def unsubscribe(*channels)
  raise "Can't unsubscribe if not subscribed." unless subscribed?

  synchronize do |_client|
    _subscription(:unsubscribe, 0, channels, nil)
  end
end