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

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
Kagamiiiii
Student &amp; New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
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

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New

We're in Beta

About us Mission Statement