File: pubsub.rb

package info (click to toggle)
ruby-redis 5.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,160 kB
  • sloc: ruby: 11,445; makefile: 117; sh: 24
file content (81 lines) | stat: -rw-r--r-- 2,994 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# frozen_string_literal: true

class Redis
  module Commands
    module Pubsub
      # Post a message to a channel.
      def publish(channel, message)
        send_command([:publish, channel, message])
      end

      def subscribed?
        !@subscription_client.nil?
      end

      # Listen for messages published to the given channels.
      def subscribe(*channels, &block)
        _subscription(:subscribe, 0, channels, block)
      end

      # Listen for messages published to the given channels. Throw a timeout error
      # if there is no messages for a timeout period.
      def subscribe_with_timeout(timeout, *channels, &block)
        _subscription(:subscribe_with_timeout, timeout, channels, block)
      end

      # Stop listening for messages posted to the given channels.
      def unsubscribe(*channels)
        _subscription(:unsubscribe, 0, channels, nil)
      end

      # Listen for messages published to channels matching the given patterns.
      # See the [Redis Server PSUBSCRIBE documentation](https://redis.io/docs/latest/commands/psubscribe/)
      # for further details
      def psubscribe(*channels, &block)
        _subscription(:psubscribe, 0, channels, block)
      end

      # Listen for messages published to channels matching the given patterns.
      # Throw a timeout error if there is no messages for a timeout period.
      # See the [Redis Server PSUBSCRIBE documentation](https://redis.io/docs/latest/commands/psubscribe/)
      # for further details
      def psubscribe_with_timeout(timeout, *channels, &block)
        _subscription(:psubscribe_with_timeout, timeout, channels, block)
      end

      # Stop listening for messages posted to channels matching the given patterns.
      # See the [Redis Server PUNSUBSCRIBE documentation](https://redis.io/docs/latest/commands/punsubscribe/)
      # for further details
      def punsubscribe(*channels)
        _subscription(:punsubscribe, 0, channels, nil)
      end

      # Inspect the state of the Pub/Sub subsystem.
      # Possible subcommands: channels, numsub, numpat.
      def pubsub(subcommand, *args)
        send_command([:pubsub, subcommand] + args)
      end

      # Post a message to a channel in a shard.
      def spublish(channel, message)
        send_command([:spublish, channel, message])
      end

      # Listen for messages published to the given channels in a shard.
      def ssubscribe(*channels, &block)
        _subscription(:ssubscribe, 0, channels, block)
      end

      # Listen for messages published to the given channels in a shard.
      # Throw a timeout error if there is no messages for a timeout period.
      def ssubscribe_with_timeout(timeout, *channels, &block)
        _subscription(:ssubscribe_with_timeout, timeout, channels, block)
      end

      # Stop listening for messages posted to the given channels in a shard.
      def sunsubscribe(*channels)
        _subscription(:sunsubscribe, 0, channels, nil)
      end
    end
  end
end