1
votes

I am creating messaging app where app A need to send message to app B using rabbitMQ. I am developing using Phoenix and on rabbitMQ web site I found tutorial how to implement rabbitmq with elixir but I don't know how to use it in my Phoenix app. I tried to add code, which I find on rabbit tutorial web site, to my Phoenix page_controller.ex

  defmodule ApplicationA.PageController do
  use ApplicationA.Web, :controller
  use AMQP

  def index(conn, _params) do
    {:ok, connection} = AMQP.Connection.open
    {:ok, channel} = AMQP.Channel.open(connection)

    AMQP.Queue.declare(channel, "hello")

    AMQP.Basic.publish(channel, "", "hello", "Hello World!")
    IO.puts " [x] Sent 'Hello World!'"

    AMQP.Connection.close(connection)

    render conn, "index.html"
  end
end

but I get this error

no match of right hand side value: {:error, :econnrefused}

in line 6

{:ok, connection} = AMQP.Connection.open

Con somebody help me how I should do this on the good way?

1
Is RabbitMQ running on localhost port 5672? - Dogbert

1 Answers

1
votes

You need to specify connection settings for RabbitMQ.

Add in your config/dev.exs something like:

config :my_app, :rabbitmq,
  host: "${RABBITMQ_HOSTNAME}", 
  port: 5672, 
  username: "${RABBITMQ_UID}", 
  password: "${RABBITMQ_PWD}"

and then load them:

rabbit_settings = Application.get_env :my_app, :rabbitmq
{:ok, connection} = AMQP.Connection.open(rabbit_settings)