gobridge

0022 — Speak MQTT 3.1.1 by translating the wire below the MQTT 5 client

Status: accepted Date: 2026-10-08 Deciders: GoBridge core Amended by: 0023 (a reconnect the broker resumed replays no retained messages), 0024 (switching the protocol version keeps the broker state key and the managed subscription history, and a live reload no longer refuses it) Relates to: 0010 (no_local is rejected on MQTT 3.1.1, so loop prevention never disappears without notice), 0011 (on MQTT 3.1.1 a short-lived connection feeds the same takeover streak and penalty), 0021 (the translator reports what it refuses through the same pre-decode reject path), 0003 (switching the protocol version of a durable session is a durable-identity change for the managed-subscription history)

Context

The mqtt transport (adapters/mqtt/transport/paho) spoke MQTT 5.0 only. It is built on github.com/eclipse/paho.golang v0.23.0, which writes protocol level 5 into every CONNECT. The library’s maintainers have declined MQTT 3.1.1 support (eclipse/paho.golang#6, #89 and #273; #104 was closed unmerged).

Brokers and managed services that speak only MQTT 3.1.1 could therefore not be bridged. Examples are Azure IoT Hub, legacy device fleets, and brokers configured for 3.1.1.

MQTT 3.1.1 and MQTT 5 are close on the wire. They differ in:

MQTT 3.1 (protocol name MQIsdp, level 3) is a third wire format, different from 3.1.1 (protocol name MQTT, level 4).

Decision

The mqtt transport speaks MQTT 3.1.1 when options.session.protocol_version is v3.1.1. A translator between Paho and the socket turns the MQTT 5 packets Paho writes into MQTT 3.1.1, and the broker’s MQTT 3.1.1 packets back into MQTT 5. Paho, the pre-decode guard, the router, delivery, reconcile and every session behaviour keep MQTT 5 semantics. An option MQTT 3.1.1 cannot express is rejected when the configuration is validated. A packet it cannot express fails the write and drops the connection. Behaviour that MQTT 3.1.1 can only offer in a weaker form is documented and warned once at session start.

Configuration

Option Why it is rejected
no_local: true MQTT 3.1.1 has no No-Local. Loop prevention must not disappear without notice (ADR 0010).
session_expiry_interval other than 0 MQTT 3.1.1 cannot send it. The broker decides how long a session lives.
password without username MQTT 3.1.1 forbids it (MQTT-3.1.2-22).
clean_start: true with session_mode: persistent No MQTT 3.1.1 wire form exists (see the Clean Session mapping below). Exclusive already forces clean_start to false, and Ephemeral ignores it.

Where the translator sits

flowchart LR
  P[Paho<br/>writes MQTT 5] -- MQTT 5 bytes --> G[pre-decode guard<br/>mqttIngressConn]
  G -- MQTT 5 bytes --> T[translator<br/>mqtt311Conn]
  T -- MQTT 3.1.1 bytes --> S[TCP / TLS / WebSocket]
  S --> B[(broker)]

Outbound: Paho → broker

Paho serialises every packet write through the guard’s lock, so the bytes of two packets never interleave. The translator buffers bytes until it holds one whole MQTT 5 packet, translates it, and writes the MQTT 3.1.1 form. A packet with no MQTT 3.1.1 form fails the write, and the connection drops. Nothing is weakened without notice.

MQTT 5 packet MQTT 3.1.1 output
CONNECT Protocol name MQTT, level 4. Properties and will properties are dropped. Clean Session is derived as shown below. A password without a username is an error.
PUBLISH Fixed header, topic, packet id (QoS above 0) and payload. Properties are dropped. An empty topic (a topic alias) is an error.
PUBACK, PUBREC, PUBREL, PUBCOMP Fixed header and packet id. Reason code and properties are dropped. PUBREL keeps flags 0x2. Paho’s PUBREL with reason 0x92 for an unknown PUBREC becomes a plain PUBREL.
SUBSCRIBE Packet id, then each filter with its requested QoS. No Local or Retain As Published is an error. Retain Handling and properties are dropped.
UNSUBSCRIBE Packet id and filters. The translator records how many filters the packet id carried, for the UNSUBACK.
PINGREQ Unchanged.
DISCONNECT, reason 0x00 MQTT 3.1.1 DISCONNECT. The broker discards the Last Will, as an MQTT 5 broker does for 0x00.
DISCONNECT, any other reason Nothing is written.
AUTH Error. Paho never sends it today.

Why a non-zero DISCONNECT writes nothing. Its only senders are the guard’s reject (0x95, 0x81) and Paho’s own error paths, and in both cases the caller closes the socket next. An MQTT 5 broker publishes the Last Will after a DISCONNECT with any non-zero reason (MQTT-3.1.2-8). An MQTT 3.1.1 broker publishes it after an unclean close, but discards it after any DISCONNECT. Writing nothing keeps the outcome the same.

Clean Session mapping. The inputs are the MQTT 5 CONNECT’s Clean Start flag and its Session Expiry Interval (absent means 0).

Clean Start Session Expiry Interval Clean Session Who produces it
1 0 1 Ephemeral, on every connect
0 above 0 0 Persistent and Exclusive, including recovery connects
0 0 1 Not produced today. In MQTT 5 it means “resume, then end the session on close”; the nearest 3.1.1 form ends the session at once.
1 above 0 error Persistent with clean_start: true. Rejected at validation; the translator fails closed if it is reached anyway.

autopaho sends Clean Start only on the first connection of a connection manager. Every reconnect sends 0, and the table maps that correctly.

Inbound: broker → Paho

The translator frames one MQTT 3.1.1 packet at a time. It validates the fixed header and the Remaining Length before it allocates anything, and hands out the equivalent MQTT 5 bytes.

Exact MQTT 3.1.1 validation. This is a trust boundary. Paho’s MQTT 5 decoder accepts bytes that are not valid MQTT 3.1.1: 40 03 00 01 87, for example, decodes as an MQTT 5 PUBACK with reason “Not authorized”. Each rule therefore stands on its own. These are malformed:

MQTT 3.1.1 packet MQTT 5 output
CONNACK The same flags, the return code mapped as below, and empty properties.
PUBLISH A recomputed Remaining Length and empty properties. The in-flight rules below apply first.
PUBACK, PUBREC, PUBREL, PUBCOMP Unchanged. The 2-byte form is valid MQTT 5 and means success with no properties.
SUBACK Empty properties, then the return codes unchanged. 0x00, 0x01, 0x02 and 0x80 mean the same in MQTT 5.
UNSUBACK Empty properties, then one Success (0x00) for each filter the UNSUBSCRIBE carried. An unknown packet id is malformed.
PINGRESP Unchanged.
MQTT 3.1.1 CONNACK return code MQTT 5 reason code
0, connection accepted 0x00
1, unacceptable protocol version 0x84, unsupported protocol version
2, identifier rejected 0x85, client identifier not valid
3, server unavailable 0x88, server unavailable
4, bad user name or password 0x86, bad user name or password
5, not authorized 0x87, not authorized
any other value malformed

The translated CONNACK carries no properties, so Paho uses the MQTT 5 defaults: Receive Maximum 65535, Maximum QoS 2, retain and shared subscriptions available, and no Maximum Packet Size. The existing CONNACK handling (error classification, ErrNotAuthorized, resume-loss detection) works unchanged.

Inbound in-flight set.

Oversized PUBLISH.

Violations. A malformed packet, a packet of any other type above the guard’s maximum packet size, and a window overflow take the guard’s reporting path, Session.rejectPredecodeIngress (ADR 0021): it counts MQTTIngressRejected, sets the service level to None, applies the reject-streak backoff and starts the readiness hold-down. The translator then closes the raw connection without writing a DISCONNECT, so the broker publishes the Last Will. Later reads return the same error.

Version checks above the translator

Above the translator, the session checks the protocol version only for these:

The transport kind stays mqtt, so no core Go code changes. Every core rule keyed on the kind keeps working: max_mqtt_sessions, the managed-subscription store and its seeding, the durable identity snapshot, clustered $share validation, and the registry and plugin checks.

Consequences

These behave as on MQTT 5: ack after settlement in receive order; QoS 0, 1 and 2; session resume and resume-loss detection from Session Present; startup grace, covered-retain and orphan cleanup; the managed-subscription history; settlement recovery and unit-rebuild containment; the poison escape; credential rotation; TLS, WebSocket and proxies; multi-URL failover; keep-alive; Last Will; the cluster and lease rules; client_id_suffix; QoS downgrade detection (SUBACK carries the granted QoS); max_mqtt_sessions; and the circuit breaker.

These degrade. Each is documented in MQTT 3.1.1 and named in the startup warning where it applies.

Behaviour On MQTT 3.1.1
Headers and identity Only topic, payload, QoS and RETAIN cross the wire. On egress every header, the subject and the expiry are dropped before validation. On ingress every message gets a minted id and is count-less (see below), unless the session opts into a content-hash id (see the 2026-10-08 addendum).
Session takeover Seen only as a connection that drops within 30 s of coming up. It feeds the takeover streak and penalty (1 s doubling to 64 s) without counting MQTTSessionTakeover. The exclusive lease is still the owner guarantee.
Publish refusal PUBACK and PUBREC carry no reason code. A broker that refuses a publish acks it (Mosquitto) or closes the connection, which surfaces as ErrConnectionLost (retryable). throttle_retry_after never applies.
Subscribe refusal SUBACK 0x80 is the only failure code. It keeps its MQTT 5 meaning, ErrUnavailable (transient), so a broker ACL denial is not classified ErrForbidden.
Retained replay No Retain Handling. A reconnect the broker resumed sends no SUBSCRIBE for an unchanged filter, so it replays nothing (ADR 0023). Retained messages are delivered again, marked mqtt.retained=true, on a reconnect the broker did not resume (a fresh or lost session), on the first connection after a process start, and on a QoS re-check.
Flow control receive_maximum is not sent; the translator enforces it. The broker’s per-client in-flight limit must not exceed it (defaults: Mosquitto max_inflight_messages 20, EMQX max_inflight 32, AWS IoT 100). A broker that sends more is refused on every connection, with nothing acked and the remedy logged. The session never wedges.
Maximum packet size Not advertised. An oversized PUBLISH is acked and dropped instead of being refused by the broker. Egress has no broker limit to check, so its cap falls back to the protocol maximum.
UNSUBACK detail Every filter reports Success. Managed cleanup always takes its connection-recycle path. A broker that refuses an UNSUBSCRIBE anyway looks like success, so the broker must permit UNSUBSCRIBE for every filter the session subscribes.
Session lifetime Broker policy (defaults: Mosquitto never expires a session, EMQX after 2 h, AWS IoT after 1 h). session_expiry_interval is rejected.
Shared subscriptions $share/ depends on the broker. Mosquitto, EMQX, HiveMQ, VerneMQ and AWS IoT accept it from 3.1.1 clients; Azure Event Grid disconnects the client. shared_consumer stays declared, because capabilities are per transport, not per configuration.
Error classification CONNACK return codes 1–5 map to the nearest MQTT 5 code. There is no server-busy (0x89) or quota (0x97) signal.

Carrying no headers has these consequences on ingress:

Not available, and nothing to reject, because the adapter never sets them: topic alias, subscription identifiers, enhanced authentication (AUTH), request/response information, server reference and will properties.

Not possible on MQTT 3.1.1: a session expiry interval, broker-side message expiry, publish refusal verdicts, a takeover reason, and header carriage without changing the payload.

Switching a Persistent or Exclusive session between v5 and v3.1.1 is a durable-identity change. The Supervisor and the AWS runtime refuse it as a live reload, and the managed-subscription history needs the documented migration (managed-filter migration). Drain the backlog before switching. For an Ephemeral session a protocol change is an ordinary rebuild, because the typed configuration is part of the reload unit’s content identity.

Rejected alternatives

Addendum 2026-10-08: opt-in content-hash message id

The Consequences above say every MQTT 3.1.1 message gets a minted id and is count-less. That stays the default. A session can now choose a content-hash id instead.

Why. An MQTT 3.1.1 PUBLISH carries only the topic, the payload, QoS, RETAIN and DUP. It has no properties, so no producer id can arrive. A minted id changes on every broker redelivery and is marked x-bridge.generated-id, so:

Decision. options.session.message_id takes random or content_hash.

An exception to the identity contract. The envelope identity contract on ports.Receiver requires source-scoped uniqueness: two distinct source messages that reach one receiver must not share an Envelope.ID. A content-hash id breaks that rule by design: two distinct messages with identical content (same session, topic and payload) collide. The operator opts into the exception per session, and random keeps the rule. The contract’s other rule, redelivery stability, holds: every redelivery to the session carries the same id, so the id is not marked generated.

Hash scope. The hash covers the broker session (client id and broker URLs), the topic and the payload. Rejected alternatives:

Consequences.

Operator guidance is on MQTT 3.1.1.