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.


42
43
44
45
46
# File 'lib/redis/commands/pubsub.rb', line 42

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.


50
51
52
53
54
# File 'lib/redis/commands/pubsub.rb', line 50

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.


67
68
69
# File 'lib/redis/commands/pubsub.rb', line 67

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

#punsubscribe(*channels) ⇒ Object

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


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

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

    client.punsubscribe(*channels)
  end
end

#subscribe(*channels, &block) ⇒ Object

Listen for messages published to the given channels.


18
19
20
21
22
# File 'lib/redis/commands/pubsub.rb', line 18

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.


26
27
28
29
30
# File 'lib/redis/commands/pubsub.rb', line 26

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
14
15
# File 'lib/redis/commands/pubsub.rb', line 11

def subscribed?
  synchronize do |client|
    client.is_a? SubscribedClient
  end
end

#unsubscribe(*channels) ⇒ Object

Stop listening for messages posted to the given channels.


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

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

    client.unsubscribe(*channels)
  end
end