EmqttFailover.Connection (EmqttFailover v0.3.1)

Wraps an :emqtt client.

Summary

Functions

Returns a specification to start this module under a supervisor.

Publishes a message to the broker.

Publishes a message to the broker without waiting for a reply.

Returns the current connection status, along with the configuration for the connection.

Gracefully stops the connection.

Types

@type backoff() ::
  non_neg_integer()
  | {non_neg_integer(), non_neg_integer()}
  | {non_neg_integer(), non_neg_integer(), :normal | :jitter}
@type config() :: EmqttFailover.Config.t() | String.t()
@type handler() :: module() | {module(), term()}
@type param() ::
  {:backoff, backoff()}
  | {:configs, Enumerable.t(config()) | config()}
  | {:client_id, binary()}
  | {:handler, handler()}
  | GenServer.option()
@type status() :: :not_connected | :connected

Functions

Link to this function

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function

publish(server, message, timeout \\ :infinity)

@spec publish(GenServer.server(), EmqttFailover.Message.t(), timeout()) ::
  :ok | {:error, term()}

Publishes a message to the broker.

For more on the message, see EmqttFailover.Message.

For qos: 0 messages, this always returns :ok

If the message has qos: 1 or qos: 2 and the server is not connected or the message is not acknowledged, this will return an error.

Link to this function

publish_async(server, message)

@spec publish_async(GenServer.server(), EmqttFailover.Message.t()) :: :ok

Publishes a message to the broker without waiting for a reply.

For more on the message, see EmqttFailover.Message.

Link to this function

start_link(config_or_opts)

@spec start_link(config() | [param()]) :: GenServer.on_start()

Start a Elixir.EmqttFailover.Connection.

Required parameters:

Optional parameters:

  • backoff: either a single number of milliseconds to wait after a connection failure, a tuple {start, max} where start and max are the initial and maximum number of milliseconds to wait, or {start, max, :jitter} to add some random jitter to the specific reconnection times. Note: this backoff timeout will only be used after all the failover connections have been tried.

  • client_id: a binary to be used as the identifier for this client

  • handler: either a module implementing EmqttFailover.ConnectionHandler or a tuple {module, init_args} where init_args are passed to the module's init/1 function.

@spec status(GenServer.server()) :: {status(), EmqttFailover.Config.t()}

Returns the current connection status, along with the configuration for the connection.

If the status is :not_connected, the config that is returned is the next one that will be tried.

Link to this function

stop(server, timeout \\ :infinity)

@spec stop(GenServer.server(), timeout()) :: :ok

Gracefully stops the connection.