Module: RMessage::Subscriber

Defined in:
lib/rmessage/subscriber.rb

Overview

Utility functions to allow setting up and parsing messages received from Redis publishers.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#channelString, ... (readonly)

Returns the channel to subscribe to.

Returns:

  • (String, Symbol, Integer)

    the channel to subscribe to



7
8
9
# File 'lib/rmessage/subscriber.rb', line 7

def channel
  @channel
end

#eventsHash (readonly)

Returns a map of event labels to classes.

Returns:

  • (Hash)

    a map of event labels to classes



11
12
13
# File 'lib/rmessage/subscriber.rb', line 11

def events
  @events
end

Instance Method Details

#setup_subscription(channel: @channel, auto_parse: false, logger: nil, connection: nil, connection_index: 0, &_) ⇒ Object

Setup a subscription to a specific Redis channel

Parameters:

  • channel (Symbol, String, Integer) (defaults to: @channel)

    the channel

  • auto_parse (Boolean) (defaults to: false)

    should messages be parsed automatically?

  • connection (Redis) (defaults to: nil)

    optional argument to provide the redis connection.

  • connection_index (Integer) (defaults to: 0)

    the index of the redis connection in CONNECTION_POOL



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rmessage/subscriber.rb', line 18

def setup_subscription(channel: @channel, auto_parse: false, logger: nil, connection: nil, connection_index: 0, &_)
  redis_connection = connection || RMessage::CONNECTION_POOL[connection_index]
  redis_connection.subscribe(channel) do |event|
    event.subscribe do
      logger&.info("Subscribed to channel [channel: #{channel}]")
    end

    event.message do |_, message|
      auto_parse ? yield(parse_message(message)) : yield(message)
      logger&.info("Received Message: #{message}")
    end
  end
end