View Source RabbitMQStream.Connection behaviour (rabbitmq_stream v0.2.1)

Responsible for encoding and decoding messages, opening and maintaining a socket connection to a single node. It connects to the RabbitMQ, and authenticates, and mantains the connection open with heartbeats.

adding-a-connectiong-to-the-supervision-tree

Adding a connectiong to the supervision tree

You can define a connection with:

defmodule MyApp.MyConnection
  use RabbitMQStream.Connection
end

Then you can add it to your supervision tree:

def start(_, _) do
  children = [
    {MyApp.MyConnection, username: "guest", password: "guest", host: "localhost", vhost: "/"},
    # ...
  ]

  opts = # ...
  Supervisor.start_link(children, opts)
end

connection-configuration

Connection configuration

The connection accept the following options:

  • username - The username to use for authentication. Defaults to guest.
  • password - The password to use for authentication. Defaults to guest.
  • host - The host to connect to. Defaults to localhost.
  • port - The port to connect to. Defaults to 5552.
  • vhost - The virtual host to use. Defaults to /.
  • frame_max - The maximum frame size in Bytes. Defaults to 1_048_576.
  • heartbeat - The heartbeat interval in seconds. Defaults to 60.
  • lazy - If true, the connection won't starting until explicitly calling connect/1. Defaults to false.

subscribing-to-messages

Subscribing to messages

You can subscribe to messages by calling subscribe/5:

{:ok, _subscription_id} = MyApp.MyConnection.subscribe("stream-01", self(), :next, 999)

configuration

Configuration

The configuration for the connection can be set in your config.exs file:

config :rabbitmq_stream, MyApp.MyConnection,
  username: "guest",
  password: "guest"
  # ...

You can override each configuration option by manually passing each configuration on the use macro:

defmodule MyApp.MyConnection
  use RabbitMQStream.Connection, username: "guest", password: "guest"
end

or when adding to the supervision tree:

def start(_, _) do
  children = [
    {MyApp.MyConnection, username: "guest", password: "guest"},
    # ...
  ]

  opts = # ...
  Supervisor.start_link(children, opts)
end

The precedence order is is the same order as the examples above, from top to bottom.

Link to this section Summary

Link to this section Types

@type connection_option() ::
  {:username, String.t()}
  | {:password, String.t()}
  | {:host, String.t()}
  | {:port, non_neg_integer()}
  | {:vhost, String.t()}
  | {:frame_max, non_neg_integer()}
  | {:heartbeat, non_neg_integer()}
  | {:lazy, boolean()}
@type connection_options() :: [connection_option()]
@type offset() ::
  :first
  | :last
  | :next
  | {:offset, non_neg_integer()}
  | {:timestamp, integer()}
@type t() :: %RabbitMQStream.Connection{
  brokers: %{required(integer()) => BrokerData.t()},
  buffer: RabbitMQStream.Message.Buffer.t(),
  connect_requests: [pid()],
  connection_properties: %{required(String.t()) => String.t() | integer()},
  correlation_sequence: integer(),
  mechanisms: [String.t()],
  options: connection_options(),
  peer_properties: [[String.t()]],
  publisher_sequence: non_neg_integer(),
  request_tracker: %{required({atom(), integer()}) => {pid(), any()}},
  socket: :gen_tcp.socket(),
  state: :closed | :connecting | :open | :closing,
  streams: %{required(String.t()) => StreamData.t()},
  subscriber_sequence: non_neg_integer(),
  subscriptions: %{required(non_neg_integer()) => pid()},
  version: 1
}

Link to this section Callbacks

@callback close(reason :: String.t(), code :: integer()) ::
  :ok | {:error, reason :: atom()}
@callback connect() :: :ok | {:error, reason :: atom()}
Link to this callback

create_stream(t, keyword)

View Source
@callback create_stream(
  String.t(),
  keyword(String.t())
) :: :ok | {:error, reason :: atom()}
@callback declare_publisher(String.t(), String.t()) ::
  {:ok, publisher_id :: integer()} | {:error, any()}
Link to this callback

delete_publisher(publisher_id)

View Source
@callback delete_publisher(publisher_id :: integer()) :: :ok | {:error, reason :: atom()}
@callback delete_stream(String.t()) :: :ok | {:error, reason :: atom()}
Link to this callback

publish(integer, integer, binary)

View Source
@callback publish(integer(), integer(), binary()) :: :ok
@callback query_metadata([String.t(), ...]) ::
  {:ok, metadata :: %{brokers: any(), streams: any()}}
  | {:error, reason :: atom()}
@callback query_offset(String.t(), String.t()) ::
  {:ok, offset :: integer()} | {:error, reason :: atom()}
Link to this callback

query_publisher_sequence(t, t)

View Source
@callback query_publisher_sequence(String.t(), String.t()) ::
  {:ok, sequence :: integer()} | {:error, reason :: atom()}
@callback start_link([connection_option() | {:name, atom()}]) ::
  :ignore | {:error, any()} | {:ok, pid()}
Link to this callback

store_offset(t, t, integer)

View Source
@callback store_offset(String.t(), String.t(), integer()) :: :ok
Link to this callback

subscribe(stream_name, pid, offset, credit, properties)

View Source
@callback subscribe(
  stream_name :: String.t(),
  pid :: pid(),
  offset :: offset(),
  credit :: non_neg_integer(),
  properties :: %{required(String.t()) => String.t()}
) :: {:ok, subscription_id :: non_neg_integer()} | {:error, reason :: atom()}
Link to this callback

unsubscribe(subscription_id)

View Source
@callback unsubscribe(subscription_id :: non_neg_integer()) ::
  :ok | {:error, reason :: atom()}