ExMqtt.State.TopicAlias (ex_mqtt_uv v0.1.0)

Copy Markdown View Source

MQTT 5.0 bidirectional Topic Alias table.

  • inbound maps broker-assigned alias integers to topic names (bounded by the client's topic_alias_maximum).
  • outbound maps topic names to client-assigned alias integers (bounded by the broker's topic_alias_maximum received in CONNACK).

Summary

Functions

Applies automatic outbound Topic Alias compression for a PUBLISH packet.

Creates a fresh TopicAlias table (reset on every new network connection).

Resets all alias mappings (required by MQTT 5.0 spec on disconnect/reconnect).

Resolves an incoming PUBLISH topic and optional :topic_alias property.

Types

t()

@type t() :: %ExMqtt.State.TopicAlias{
  inbound: %{optional(pos_integer()) => String.t()},
  inbound_max: non_neg_integer(),
  next_outbound_alias: pos_integer(),
  outbound: %{optional(String.t()) => pos_integer()},
  outbound_max: non_neg_integer()
}

Functions

apply_outbound(state, topic, props)

@spec apply_outbound(t(), String.t(), map()) :: {String.t(), map(), t()}

Applies automatic outbound Topic Alias compression for a PUBLISH packet.

  • If the broker advertised outbound_max == 0 or the caller already set :topic_alias, the topic and properties are left unchanged.
  • On first publish of a topic (while next_outbound_alias <= outbound_max), registers the alias and keeps the full topic name + topic_alias: alias_id.
  • On subsequent publishes of that same topic on the same connection, replaces the wire topic with "" and sets topic_alias: alias_id.

new(inbound_max \\ 0, outbound_max \\ 0)

@spec new(non_neg_integer(), non_neg_integer()) :: t()

Creates a fresh TopicAlias table (reset on every new network connection).

reset(topic_alias, outbound_max \\ 0)

@spec reset(t(), non_neg_integer()) :: t()

Resets all alias mappings (required by MQTT 5.0 spec on disconnect/reconnect).

resolve_inbound(state, topic, alias_id)

@spec resolve_inbound(t(), String.t(), pos_integer() | nil) ::
  {:ok, String.t(), t()} | {:error, :topic_alias_invalid}

Resolves an incoming PUBLISH topic and optional :topic_alias property.