MqttX.Packet.Codec (MqttX v0.10.0)

Copy Markdown View Source

High-performance MQTT packet encoder and decoder.

Supports MQTT 3.1, 3.1.1, and 5.0 protocols with all 15 packet types.

Encoding

packet = %{type: :publish, topic: "test", payload: "hello", qos: 0, retain: false}
{:ok, binary} = MqttX.Packet.Codec.encode(4, packet)

Decoding

{:ok, {packet, rest}} = MqttX.Packet.Codec.decode(4, binary)

Summary

Functions

Decode an MQTT packet from binary data.

Encode an MQTT packet to binary.

Encode an MQTT packet to iodata (more efficient, avoids binary copy).

Functions

decode(version, arg2)

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

Decode an MQTT packet from binary data.

Returns {:ok, {packet, rest}} on success, {:error, reason} on failure, or {:error, :incomplete} if more data is needed.

encode(version, packet)

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

Encode an MQTT packet to binary.

Returns {:ok, binary} on success or {:error, reason} on failure.

encode_iodata(version, packet)

@spec encode_iodata(integer(), map()) :: {:ok, iodata()} | {:error, atom()}

Encode an MQTT packet to iodata (more efficient, avoids binary copy).