Class: LontaraUtilities::RMQ::Client::Publisher

Inherits:
Object
  • Object
show all
Defined in:
lib/lontara_utilities/rmq/client/publisher.rb

Overview

Client for AMQ on Pub/Sub Pattern

Instance Method Summary collapse

Constructor Details

#initialize(connection, queue:, exchange: nil, exchange_type: :direct) ⇒ Publisher

Returns a new instance of Publisher.



8
9
10
11
12
13
14
15
16
17
# File 'lib/lontara_utilities/rmq/client/publisher.rb', line 8

def initialize(connection, queue:, exchange: nil, exchange_type: :direct)
  @channel = connection.channel

  # Use `default_exchange` if exchange is not declared in parameter
  # nor use the specified exchange from the connection.
  x = -> { exchange.nil? ? channel.default_exchange : channel.exchange(exchange, type: exchange_type) }
  @exchange = connection.exchange.nil? ? x.call : connection.exchange

  @queue = channel.queue(queue, durable: true)
end

Instance Method Details

#publish(&block) ⇒ Object

Publish message to the queue.

This method will yield the block to get the message. Message published to NestJS Service must contain ‘id` key.

Example:

“‘ client.publish do

{
  id: 'message_1',
  pattern: 'voucher.voucher.find_one',
  data: { id: 1 }
}

end “‘



35
36
37
38
39
40
41
42
# File 'lib/lontara_utilities/rmq/client/publisher.rb', line 35

def publish(&block)
  @request = block.call

  exchange.publish(
    request.to_json,
    routing_key: queue.name
  )
end