fireproofsocks

fireproofsocks

RabbitMQ/amqp noob: naming connections/channels?

I’m starting to work with RabbitMQ and the amqp package and I have a couple noob questions that I’m hoping someone can set me straight on…

First: is it possible to name the RabbitMQ connection PID? I’m used to Phoenix.PubSub where you can start one or more processes and refer to them by the named pid so that messages can be broadcast/published to that instance. In amqp, there is a :name option, but the docs specifically state that it is not unique and cannot be used as a connection identifier. I’m guessing that this is intentional due to some specifics about how RabbitMQ operates, but can anyone shed light on this? How can easily send/publish/broadcast messages? Is the recommended method to open up a new connection as needed? (Instead of holding onto a named connection pid).

Secondly: is there a way to create topics (i.e. open a channel) when the app starts? I’m seeing warnings when parts of the app subscribe to topics that don’t exist yet. I can manually create these channels and the logs quiet down – this is different from Phoenix.PubSub which lets you subscribe to any topic name you want regardless if it has been formally “initialized” or not. Is it advisable to pre-define these channel names (i.e. topic names) when the app starts? Or is that not recommended?

I’m wanting to be more simplistic about sending messages, something more like

# wishful thinking pseudo-code? 
Rabbit.send(:instance_name, "my-topic", "my-message", my_options)

Thanks for any pointers! Sorry if my nomenclature and way of thinking about this is all from the Phoenix.PubSub point of view, but that has been the bulk of experience.

First Post!

dimitarvp

dimitarvp

I mean, the following is straight from the root page of the HexDocs page of amqp:

{:ok, conn} = AMQP.Connection.open()
# {:ok, %AMQP.Connection{pid: #PID<0.165.0>}}

{:ok, chan} = AMQP.Channel.open(conn)
# {:ok, %AMQP.Channel{conn: %AMQP.Connection{pid: #PID<0.165.0>}, pid: #PID<0.177.0>}

AMQP.Queue.declare(chan, "test_queue")
# {:ok, %{consumer_count: 0, message_count: 0, queue: "test_queue"}}

AMQP.Exchange.declare(chan, "test_exchange")
# :ok

AMQP.Queue.bind(chan, "test_queue", "test_exchange")
# :ok

AMQP.Basic.publish(chan, "test_exchange", "", "Hello, World!")
# :ok

{:ok, payload, meta} = AMQP.Basic.get(chan, "test_queue")
payload
# "Hello, World!"

AMQP.Queue.subscribe(chan, "test_queue", fn payload, _meta -> IO.puts("Received: #{payload}") end)
# {:ok, "amq.ctag-5L8U-n0HU5doEsNTQpaXWg"}

AMQP.Basic.publish(chan, "test_exchange", "", "Hello, World!")
# :ok
# Received: Hello, World!

Does that not cover your use-case?


Not sure if it’s the same thing but does the configuration section not cover this?

You can define a connection and channel in your config and AMQP will automatically…

  • Open the connection and channel at the start of the application
  • Automatically try to reconnect if they are disconnected
config :amqp,
  connections: [
    myconn: [url: "amqp://guest:guest@myhost:12345"],
  ],
  channels: [
    mychan: [connection: :myconn]
  ]

Where Next?

Popular in Questions Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
gazoon
I want to know absolute current module path. In python i could do that: os.path.abspath(__file__) Does elixir have anything similar?
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement