Overview

rabbit_mq is an opinionated RabbitMQ client to help you build balanced and consistent Consumers and Producers.

Documentation

The full documentation is published on hex.

The following modules are provided;

Example usage

  1. Define network topology
  2. Define Producers
  3. Define Consumers
  4. Supervise
  5. Start producing and consuming messages

Define network topology

defmodule Topology do
  use RabbitMQ.Topology,
    exchanges: [
      {"customer", :topic,
        [
          {"customer.created", "customer/customer.created", durable: true},
          {"customer.updated", "customer/customer.updated", durable: true},
          {"#", "customer/#"}
        ], durable: true}
    ]
end

Define Producers

defmodule CustomerProducer do
  use RabbitMQ.Producer, exchange: "customer", worker_count: 3

  def customer_updated(updated_customer) when is_map(updated_customer) do
    opts = [
      content_type: "application/json",
      correlation_id: UUID.uuid4(),
      mandatory: true
    ]

    payload = Jason.encode!(updated_customer)

    publish(payload, "customer.updated", opts)
  end
end

Define Consumers

defmodule CustomerConsumer do
  use RabbitMQ.Consumer, queue: "customer/customer.updated", worker_count: 3

  require Logger

  def consume(payload, meta, channel) do
    Logger.info(payload)
    ack(channel, meta.delivery_tag)
  end
end

Supervise

Start as normal under your existing supervision tree.

⚠️ Please note that the Topology module will terminate gracefully as soon as the network is configured.

children = [
  Topology,
  CustomerConsumer,
  CustomerProducer,
  # ...and more
]

Supervisor.start_link(children, strategy: :one_for_one)

Start producing and consuming messages

To publish, simply call your pre-defined methods.

CustomerProducer.customer_updated(updated_customer)

To consume, use the consume/3 implementation.

Balanced performance and reliability

The RabbitMQ modules are pre-configured with sensible defaults and follow design principles that improve and delicately balance both performance and reliability.

This has been possible through

⚠️ While most of the heavy-lifting is provided by the library itself, reading through the documents below before running any application in production is thoroughly recommended.