View Source RabbitMQStream.Connection behaviour (rabbitmq_stream v0.2.0)
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 toguest
.password
- The password to use for authentication. Defaults toguest
.host
- The host to connect to. Defaults tolocalhost
.port
- The port to connect to. Defaults to5552
.vhost
- The virtual host to use. Defaults to/
.frame_max
- The maximum frame size in Bytes. Defaults to1_048_576
.heartbeat
- The heartbeat interval in seconds. Defaults to60
.lazy
- Iftrue
, the connection won't starting until explicitly callingconnect/1
. Defaults tofalse
.
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)
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 connect() :: :ok | {:error, reason :: atom()}
@callback start_link([connection_option() | {:name, atom()}]) :: :ignore | {:error, any()} | {:ok, pid()}
@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()}
@callback unsubscribe(subscription_id :: non_neg_integer()) :: :ok | {:error, reason :: atom()}