MqttX.Payload.Protobuf (MqttX v0.9.0)

Copy Markdown View Source

Protocol Buffers payload codec using Protox.

Requires the protox optional dependency.

Unlike JSON, Protobuf requires knowing the message type for decoding. This codec provides both a generic interface and message-specific functions.

Usage

# Encoding (returns iodata by default)
{:ok, binary} = MqttX.Payload.Protobuf.encode(my_proto_struct)

# Decoding (requires message module)
{:ok, struct} = MqttX.Payload.Protobuf.decode(binary, MyMessage)

# For the behaviour interface, use with a message type in the term
{:ok, binary} = MqttX.Payload.Protobuf.encode({MyMessage, data})

Summary

Functions

Decode a Protobuf message with the specified message module.

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

Functions

decode(binary, message_module)

@spec decode(binary(), module()) :: {:ok, struct()} | {:error, term()}

Decode a Protobuf message with the specified message module.

Example

{:ok, message} = MqttX.Payload.Protobuf.decode(binary, MyProto.Message)

encode_iodata(struct)

@spec encode_iodata(struct()) :: {:ok, iodata()} | {:error, term()}

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