MqttX.Packet.Properties (MqttX v0.13.0)

Copy Markdown View Source

MQTT 5.0 Properties encoding and decoding.

Supports all 27 MQTT 5.0 property types, each checked against the packet that carries it (§2.2.2.2).

Shapes

Properties decode to a map keyed by name. :user_properties is a list of {key, value} pairs in wire order. :subscription_identifier is an integer when a packet carries one — a SUBSCRIBE always, a PUBLISH matching one subscription — and a list of integers when a PUBLISH matches several subscriptions (one identifier per subscription, §3.3.2.3.8). Every other property is a single value.

Summary

Functions

Decode properties from binary.

decode/2 for a known packet type: a property the type does not define makes the packet malformed (§2.2.2.2).

The first property in properties that packet_type does not define, or nil.

Encode properties map to binary with length prefix.

encode/2 for a known packet type: a property the type does not define (§2.2.2.2) is refused. The throw is caught at the Codec.encode boundary and becomes {:error, {:property_not_allowed, name, packet_type}}.

Types

packet_type()

@type packet_type() ::
  :connect
  | :connack
  | :publish
  | :will
  | :ack
  | :subscribe
  | :suback
  | :unsubscribe
  | :unsuback
  | :disconnect
  | :auth

Functions

decode(arg1, data)

@spec decode(integer(), binary()) :: {:ok, map(), binary()} | {:error, atom()}

Decode properties from binary.

Returns {:ok, properties_map, rest} or {:error, reason}.

decode(version, data, packet_type)

@spec decode(integer(), binary(), packet_type()) ::
  {:ok, map(), binary()} | {:error, atom()}

decode/2 for a known packet type: a property the type does not define makes the packet malformed (§2.2.2.2).

disallowed(properties, packet_type)

@spec disallowed(map(), packet_type()) :: atom() | nil

The first property in properties that packet_type does not define, or nil.

encode(arg1, properties)

@spec encode(integer(), map()) :: iodata()

Encode properties map to binary with length prefix.

Only encodes properties for MQTT 5.0 (a zero length when there are none); other versions get no bytes at all.

encode(version, properties, packet_type)

@spec encode(integer(), map(), packet_type()) :: iodata()

encode/2 for a known packet type: a property the type does not define (§2.2.2.2) is refused. The throw is caught at the Codec.encode boundary and becomes {:error, {:property_not_allowed, name, packet_type}}.