peppy
I set up a small process that maintains an AMQP connection/channel. This script automatically reconnects to RabbitMQ if the connection fails. I built this based on an old, depreciated and incomplete guide. I’m still a newbie, so I don’t fully understand how to use this. I know the script is connecting and reconnecting (as far as I know), but how do I actually use/fetch the channel?
For example, if I have a function in a different module, such as chatroom.ex, how do I fetch the existing channel in the pool so that I can publish messages to a queue?
def publish_function(chatmessage) do
# Receive a chat room message here and publish it to "test_queue" in RabbitMQ. How do I use the existing connections?
#something like:
channel = ???? #channel in the consumer module?
AMQP.Basic.publish(*channel*, "", "test_queue", chatmessage)
end
Here is what I have so far:
connectionmanager.ex:
defmodule ExchatWeb.AMQPConnectionManager do
use GenServer
use AMQP
def start_link(_opts) do
GenServer.start_link(__MODULE__, :ok, [name: __MODULE__])
end
def init(:ok) do
children = [
ExchatWeb.Publish
]
Supervisor.start_link(children, strategy: :one_for_one, name: ExchatWeb.PublishSupervisor)
establish_new_connection()
end
defp establish_new_connection do
case AMQP.Connection.open do
{:ok, conn} ->
Process.link conn.pid
{:ok, {conn, %{}}}
{:error, reason} ->
IO.puts "failed for #{inspect reason}"
:timer.sleep 5000
establish_new_connection()
end
end
def request_channel(consumer) do
GenServer.cast(__MODULE__, {:chan_request, consumer})
end
def handle_cast({:chan_request, consumer}, {conn, channel_mappings}) do
new_mapping = store_channel_mapping(conn, consumer, channel_mappings)
channel = Map.get(new_mapping, consumer)
consumer.channel_available(channel)
{:noreply, {conn, new_mapping}}
end
defp store_channel_mapping(conn, consumer, channel_mappings) do
Map.put_new_lazy(channel_mappings, consumer, fn() -> create_channel(conn) end)
IO.inspect(channel_mappings)
end
defp create_channel(conn) do
{:ok, chan} = Channel.open(conn)
chan
end
end
publishconsumer.ex:
defmodule ExchatWeb.PublishConsumer do
use GenServer
def start_link(_opts) do
GenServer.start_link(__MODULE__, :ok, [name: __MODULE__])
end
def init(_opts) do
ExchatWeb.AMQPConnectionManager.request_channel(__MODULE__)
{:ok, nil}
end
def channel_available(chan) do
GenServer.cast(__MODULE__, {:channel_available, chan})
end
def handle_cast({:channel_available, chan}, _state) do
IO.inspect(chan)
#bind_to_queue chan
{:noreply, chan}
end
end
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 2- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
peppy
Someone must know how to do this. This is basically a conceptual question that could be applied to many other Elixir modules, not just RabbitMQ and Broadway. If the consumer process and connection are linked together, how do I actually access the live connection?
I’ve been testing for awhile, I know the connection exists and re-connects, as I can see it in my RabbitMQ admin panel and can manually close the connection - and my script reconnects automatically. But I can’t seem to actually find and use this connection in the Elixir module script. Like how do I find it to publish messages to RabbitMQ without having to start a new connection for each and every message sent?
beepbeepbopbop
If I am reading this correctly, you want to checkout a channel from your connection pool and publish to said channel. Based off the code you’ve posted, I assume the following:
ConnectionManager.checkout(...)which results in getting a channel that you can use.In this instance, user land code may look like this:
This means that while
ConnectionManageris a process, you need to change it to use a synchronous API, using thecall()variants. You would also need to implement your check-in logic, unless you use a callback API where it automatically cleans up for you.Your
handle_castsimply will not work as it is now. Since your user land code assumes that a channel is something that you return, you can’t use ahandle_cast, unless you utiliseGenServer.reply, which isn’t necessary in your use case.