There has been some improvements, rabbitmq team has pushed amqp10_client to hex. it’s easier to include it to your project.
if you are given a connection string like:
Endpoint=sb://[namespace].servicebus.windows.net/;SharedAccessKeyName=MyKeyName;SharedAccessKey=MyAccessKey;EntityPath=MyEntityPath
Then you can use it in your project like
address = '[namespace].servicebus.windows.net'
hostname = to_string(address)
user = "MyKeyName"
password = "MyAccessKey"
port = 5671
queue_name = "MyEntityPath"
subscription_name = "MySubscriptionName" # This is not provided in the connection string but is an important value. With an invalid setting, you'll get "The messaging entity '....' could not be found.
opn_conf = %{
address: address,
hostname: hostname,
port: port,
container_id: subscription_name,
sasl: {:plain, user, password},
tls_opts: {:secure_port, []},
transfer_limit_margin: 100
}
{:ok, connection} = :amqp10_client.open_connection(opn_conf)
{:ok, session} = :amqp10_client.begin_session(connection)
{:ok, receiver} =
:amqp10_client.attach_receiver_link(
session,
subscription_name,
queue_name
)
:ok = :amqp10_client.flow_link_credit(receiver, 5, :never)
With this code snippet, the messages are sent to the caller’s process mailbox. For more advance usage checkout the source code
if you run it in iex, you can get the messages by running flush
:
iex(1)> MyApp.run
iex(2)> flush
{:amqp10_event, {:connection, #PID<0.230.0>, :opened}}
{:amqp10_event, {:session, #PID<0.241.0>, :begun}}
{:amqp10_event, {:link, {:link_ref, :receiver, #PID<0.241.0>, 0}, :attached}}