{"id":15647922,"url":"https://github.com/nsweeting/rabbit","last_synced_at":"2025-04-05T06:05:08.170Z","repository":{"id":46345860,"uuid":"188481559","full_name":"nsweeting/rabbit","owner":"nsweeting","description":"Build Elixir applications with RabbitMQ","archived":false,"fork":false,"pushed_at":"2024-12-13T01:57:34.000Z","size":292,"stargazers_count":42,"open_issues_count":9,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-29T05:04:26.328Z","etag":null,"topics":["broker","elixir","elixir-lang","messaging","otp","rabbitmq"],"latest_commit_sha":null,"homepage":"","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nsweeting.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-05-24T20:09:26.000Z","updated_at":"2024-12-12T22:17:32.000Z","dependencies_parsed_at":"2025-01-16T20:08:35.521Z","dependency_job_id":"bb7fdb8a-8971-4c83-a3e3-f18fadfe477f","html_url":"https://github.com/nsweeting/rabbit","commit_stats":{"total_commits":147,"total_committers":4,"mean_commits":36.75,"dds":"0.19047619047619047","last_synced_commit":"1ce0a4f96c047f5a985bafec40d3c0364faae0f1"},"previous_names":[],"tags_count":27,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nsweeting%2Frabbit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nsweeting%2Frabbit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nsweeting%2Frabbit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nsweeting%2Frabbit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nsweeting","download_url":"https://codeload.github.com/nsweeting/rabbit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247294538,"owners_count":20915340,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["broker","elixir","elixir-lang","messaging","otp","rabbitmq"],"created_at":"2024-10-03T12:22:00.963Z","updated_at":"2025-04-05T06:05:08.147Z","avatar_url":"https://github.com/nsweeting.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Rabbit\n\n[![Build Status](https://travis-ci.org/nsweeting/rabbit.svg?branch=master)](https://travis-ci.org/nsweeting/rabbit)\n[![Rabbit Version](https://img.shields.io/hexpm/v/rabbit.svg)](https://hex.pm/packages/rabbit)\n\nRabbit is a set of tools for building applications with RabbitMQ.\n\n## Installation\n\nThe package can be installed by adding `rabbit` to your list of dependencies in `mix.exs`:\n\n```elixir\ndef deps do\n  [\n    {:rabbit, \"~\u003e 0.20\"}\n  ]\nend\n```\n\n## Documentation\n\nPlease see [HexDocs](https://hexdocs.pm/rabbit) for additional documentation.\n\n## [Connections](https://hexdocs.pm/rabbit/Rabbit.Connection.html)\n\nConnections form the basis of any application that is working with RabbitMQ. A\nconnection module is needed by all the other modules included with Rabbit.\n\nUpon start, a connection will automatically create a pool of RabbitMQ connections\nto utilize.\n\n```elixir\ndefmodule MyConnection do\n  use Rabbit.Connection\n\n  def start_link(opts \\\\ []) do\n    Rabbit.Connection.start_link(__MODULE__, opts, name: __MODULE__)\n  end\n\n  # Callbacks\n\n  @impl Rabbit.Connection\n  def init(:connection_pool, opts) do\n    # Perform runtime pool config\n    {:ok, opts}\n  end\n\n  def init(:connection, opts) do\n    # Perform runtime connection config\n    uri = System.get_env(\"RABBITMQ_URI\") || \"amqp://guest:guest@127.0.0.1:5672\"\n    opts = Keyword.put(opts, :uri, uri)\n\n    {:ok, opts}\n  end\nend\n\nMyConnection.start_link()\n```\n\n## [Consumers](https://hexdocs.pm/rabbit/Rabbit.Consumer.html)\n\nConsumers are the \"workers\" of your application. They must be provided a connection\nmodule and queue to consume. Every message received is then passed along to your\n`handle_message/1` callback within its own process.\n\nYou can optionally implement the `handle_setup/2` callback to perform any work\nneeded to declare queues/exchanges/bindings.\n\n```elixir\ndefmodule MyConsumer do\n  use Rabbit.Consumer\n\n  def start_link(opts \\\\ []) do\n    Rabbit.Consumer.start_link(__MODULE__, opts, name: __MODULE__)\n  end\n\n  # Callbacks\n\n  @impl Rabbit.Consumer\n  def init(:consumer, opts) do\n    # Perform runtime config\n    {:ok, opts}\n  end\n\n  @impl Rabbit.Consumer\n  def handle_setup(state) do\n    # Optional callback to perform any exchange or queue setup\n    AMQP.Queue.declare(state.channel, state.queue)\n    :ok\n  end\n\n  @impl Rabbit.Consumer\n  def handle_message(message) do\n    # Handle message consumption\n    IO.inspect(message.payload)\n    {:ack, message}\n  end\n\n  @impl Rabbit.Consumer\n  def handle_error(message) do\n    # Handle message errors\n    {:nack, message}\n  end\nend\n\nMyConsumer.start_link(connection: MyConnection, queue: \"my_queue\", prefetch_count: 10)\n```\n\n## [Consumer Supervisors](https://hexdocs.pm/rabbit/Rabbit.ConsumerSupervisor.html)\n\nConsumer supervisors provide an easy way to start and supervise multiple consumer\nprocesses. Rather than creating a module for each consumer and implementing\nrepetitive logic - the same callbacks are used across all consumers.\n\n```elixir\ndefmodule MyConsumerSupervisor do\n  use Rabbit.ConsumerSupervisor\n\n  def start_link(consumers \\\\ []) do\n    Rabbit.ConsumerSupervisor.start_link(__MODULE__, consumers, name: __MODULE__)\n  end\n\n  # Callbacks\n\n  @impl Rabbit.ConsumerSupervisor\n  def init(:consumer_supervisor, _consumers) do\n    # Perform runtime config for the consumer supervisor\n    consumers = [\n      [connection: MyConnection, queue: \"my_queue\", prefetch_count: 5],\n      [connection: MyConnection, queue: \"my_queue_2\", prefetch_count: 10],\n    ]\n\n    {:ok, consumers}\n  end\n\n  def init(:consumer, opts) do\n    # Perform runtime config per consumer\n    {:ok, opts}\n  end\n\n  @impl Rabbit.ConsumerSupervisor\n  def handle_setup(state) do\n    # Optional callback to perform any exchange or queue setup per consumer\n    AMQP.Queue.declare(state.channel, state.queue)\n    :ok\n  end\n\n  @impl Rabbit.ConsumerSupervisor\n  def handle_message(message) do\n    # Handle message consumption per consumer\n    IO.inspect(message.payload)\n    {:ack, message}\n  end\n\n  @impl Rabbit.ConsumerSupervisor\n  def handle_error(message) do\n    # Handle message errors per consumer\n    {:nack, message}\n  end\nend\n\nMyConsumerSupervisor.start_link()\n```\n\n## [Producers](https://hexdocs.pm/rabbit/Rabbit.Producer.html)\n\nIn order to publish messages to RabbitMQ, we must create a producer module. They\nmust be provided a connection module.\n\nUpon start, a producer will automatically create a pool of RabbitMQ channels\nto publish from.\n\nYou can optionally implement the `handle_setup/1` callback to perform any work\nneeded to declare queues/exchanges/bindings.\n\n```elixir\ndefmodule MyProducer do\n  use Rabbit.Producer\n\n  def start_link(opts \\\\ []) do\n    Rabbit.Producer.start_link(__MODULE__, opts, name: __MODULE__)\n  end\n\n  # Callbacks\n\n  @impl Rabbit.Producer\n  def init(:producer_pool, opts) do\n    # Perform runtime config for the producer pool\n    {:ok, opts}\n  end\n\n  def init(:producer, opts) do\n    # Perform runtime config per producer\n    {:ok, opts}\n  end\nend\n\nMyProducer.start_link(connection: MyConnection)\nRabbit.Producer.publish(MyProducer, \"\", \"my_queue\", \"hello\")\n```\n\n## [Topology](https://hexdocs.pm/rabbit/Rabbit.Topology.html)\n\nTopology provides a way to centralize any RabbitMQ setup required by your\napplication. In that sense, it should be started BEFORE any of your producers\nor consumers.\n\nUsing a topology, you can automatically setup queues, exchanges and bindings\nwith simple keyword lists.\n\n```elixir\ndefmodule MyTopology do\n  use Rabbit.Topology\n\n  def start_link(opts \\\\ []) do\n    Rabbit.Topology.start_link(__MODULE__, opts, name: __MODULE__)\n  end\n\n  # Callbacks\n\n  @impl Rabbit.Topology\n  def init(:topology, opts) do\n    # Perform runtime config\n    {:ok, opts}\n  end\nend\n\nMyTopology.start_link(\n  connection: MyConnection,\n  queues: [\n    [name: \"my_queue\", durable: true],\n    [name: \"my_queue_2\", durable: true],\n  ],\n  exchanges: [\n    [name: \"my_exchange\"],\n    [name: \"my_exchange_2\", type: :fanout, durable: true],\n  ],\n  bindings: [\n    [type: :queue, source: \"my_exchange\", destination: \"my_queue\", routing_key: \"my_key\"],\n    [type: :exchange, source: \"my_exchange_2\", destination: \"my_exchange_1\"]\n  ]\n)\n```\n\n## [Brokers](https://hexdocs.pm/rabbit/Rabbit.Broker.html)\n\nBrokers encapsulate all of the above components into a single easy-to-use module.\nIt provides a single place to handle your RabbitMQ connections, topology,\nproducers and consumers.\n\n```elixir\ndefmodule MyBroker do\n  use Rabbit.Broker\n\n  def start_link(opts \\\\ []) do\n    Rabbit.Broker.start_link(__MODULE__, opts, name: __MODULE__)\n  end\n\n  # Callbacks\n\n  @impl Rabbit.Broker\n  # Perform runtime configuration per component\n  def init(:connection_pool, opts), do: {:ok, opts}\n  def init(:connection, opts), do: {:ok, opts}\n  def init(:topology, opts), do: {:ok, opts}\n  def init(:producer_pool, opts), do: {:ok, opts}\n  def init(:producer, opts), do: {:ok, opts}\n  def init(:consumer_supervisor, opts), do: {:ok, opts}\n  def init(:consumer, opts), do: {:ok, opts}\n\n  @impl Rabbit.Broker\n  def handle_message(message) do\n    # Handle message consumption per consumer\n    IO.inspect(message.payload)\n    {:ack, message}\n  end\n\n  @impl Rabbit.Broker\n  def handle_error(message) do\n    # Handle message errors per consumer\n    {:nack, message}\n  end\nend\n\nMyBroker.start_link(\n  connection: [uri: \"amqp://guest:guest@127.0.0.1:5672\"],\n  topology: [\n    queues: [\n      [name: \"my_queue\", durable: true],\n      [name: \"my_queue_2\", durable: true]\n    ]\n  ],\n  producer: [pool_size: 10],\n  consumers: [\n    [queue: \"my_queue\"],\n    [queue: \"my_queue_2\", prefetch_count: 10]\n  ]\n)\nRabbit.Broker.publish(MyBroker, \"\", \"my_queue\", \"hello\")\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnsweeting%2Frabbit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnsweeting%2Frabbit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnsweeting%2Frabbit/lists"}