# `MqttX.Payload.Protobuf`
[🔗](https://github.com/cignosystems/mqttx/blob/v0.9.0/lib/mqttx/payload/protobuf.ex#L1)

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})

# `decode`

```elixir
@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`

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

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

---

*Consult [api-reference.md](api-reference.md) for complete listing*
