channels v0.0.1 Channels.Publisher

A behaviour module for implementing an AMQP publisher.

A Channels.Publisher is a process that holds a channel and an exchange name, and provides functions to send messages to the AMQP broker.

It is based on a GenServer therefore include functionality for tracing and error reporting. It will also fit in a supervision tree.

Example

The Channels.Publisher behaviour hides low level details and provides a publish/4 function to publish messages.

Let’s start with a code example. Imagine we want a publisher that publishes numbers to a direct exchange “my_exchange” with a routing key tagging whether the number is positive negative or zero.

# Exchange config:
config = %{
  exchange: %{
    name: "my_exchange",
    type: "direct"
  }
}

# Start the publisher
{:ok, pid} = Channels.Publisher.start_link(config)

# Send a message
Channels.Publisher.publish(pid, "3", "positive")
Channels.Publisher.publish(pid, "0", "zero")
Channels.Publisher.publish(pid, "-2", "negative")

We start our Channels.Publisher process by calling start_link/1, passing exchange configuration.

When we start the publisher, the following steps are taking place:

1 - A GenServer is started. 2 - A new channel is created for this publisher. 3 - The configured exchange is being declared.

After that the publisher is ready to be used. We are using the publish/3 function to publish 3 messages to the AMQP broker containing “3”, “0”, and “-2” as payloads, and “positive, “zero”, and “negative” as routing keys respectively.

Name Registration

The name registration rules are the same of a GenServer.

Summary

Functions

Publishes a message to the configured exchange

Starts a new consumer without links (outside of a supervison tree)

Starts a new publisher with the given configuration

Types

opts :: [GenServer.options | {:adapter, Adapter.t} | {:context, module}]
payload :: binary
routing_key :: binary

Functions

publish(publisher, payload, routing_key \\ "", opts \\ [])

Specs

publish(pid, payload, routing_key, options) :: :ok

Publishes a message to the configured exchange.

  • pid - The publisher pid.
  • payload - The message to be published.
  • routing_key - (optional) The routing key.
  • options - (optional) Options to be passed to the adapter.
start(config, opts \\ [])

Specs

Starts a new consumer without links (outside of a supervison tree).

See start_link2 for more information.

start_link(config, opts \\ [])

Specs

start_link(config, opts) :: GenServer.on_start

Starts a new publisher with the given configuration.

  • config - The configuration of the publisher.
  • opts - GenServer options.