Skip to main content

MQTT Provider

The MQTT Provider is the Beacon Tower service that bridges a Mosquitto broker to the platform's NATS event bus. It is the canonical reference implementation built on the Provider SDK and the easiest path to connect any MQTT-speaking device.

This page covers the conceptual model: how the provider sits next to the broker, the topic conventions it enforces, the ACL plugin it depends on, how retained state and commands work over MQTT, and how device credentials are stored at rest.

For the public API surface, see Provider SDK Public Surface. For wiring a device, see Device Connectivity.

Architecture​

The provider is a sidecar to a standard Mosquitto deployment:

┌──────────────┐                      ┌───────────────────────────┐
│ │ AUTH/ACL queries │ MQTT Provider │
│ Mosquitto │◀────────────────────▶│ - Device registry │
│ + mqauth │ │ - Telemetry pump │
│ │ │ - Method dispatcher │
└──────┬───────┘ └─────────────┬─────────────┘
│ │
│ MQTT (TLS) │ NATS JetStream
▼ ▼
Devices Asset / Flow / etc.

Two transport hops, one logical pipeline:

HopProtocolAuthDirection
Device ↔ BrokerMQTT 3.1.1 / 5.0username + bcrypt password (or mTLS)Bidirectional
Provider ↔ BrokerMQTT 3.1.1 / 5.0provider service accountBidirectional
Broker ↔ ProviderPostgreSQL (auth/ACL plugin queries)DB credentialsInbound only
Provider ↔ PlatformNATS request-reply + JetStreamNATS authBidirectional

The provider holds no broker-specific state itself — Mosquitto's auth plugin, beacontower-mqauth, queries the provider's Postgres database directly for authentication and ACL decisions.

Topic conventions​

All device topics live under the reserved namespace device/{deviceId}/.... The provider enforces this both via ACL rules and by hard-coding the patterns in its adapter. The topic tree itself is in MQTT Protocol Reference › Topic Structure.

The provider holds two broker sessions and wildcard-subscribes on each, at startup and after every reconnect:

SessionSubscribes to
Adapterdevice/+/telemetry, device/+/twin/reported (QoS 1)
Commanddevice/+/command/+/response/+ (QoS 1)

It publishes twin/desired on the adapter session and command invocations on the command session.

Each inbound message is parsed into (deviceId, messageType) and forwarded into the platform's telemetry pipeline as a transport-independent payload of bytes.

ACL via the mqauth plugin​

Mosquitto delegates both authentication and authorisation to the provider's database through the Beacon Tower auth plugin (bt_mqauth.so, built from beacontower-mqauth). Two tables back the plugin:

TableStoresQueried on
mqtt_user_entityUsername + bcrypt(password) per deviceCONNECT
mqtt_acl_entityUsername + topic pattern + access levelPUBLISH / SUBSCRIBE

Each provisioned device gets one user row and five ACL rows, one per topic it may use. access_level is a bitmask: 1 = read, 2 = write, 4 = subscribe.

TopicAccessDevice may
device/{deviceId}/telemetry2 (write)Publish telemetry
device/{deviceId}/twin/reported2 (write)Publish reported properties
device/{deviceId}/command/+/response/+2 (write)Publish command responses
device/{deviceId}/twin/desired5 (read + subscribe)Receive desired properties
device/{deviceId}/command/+/invoke5 (read + subscribe)Receive command invocations

A device cannot subscribe to or publish on another device's topics, nor on any topic of its own that is not listed.

If the broker cannot reach PostgreSQL, the plugin fails closed: a CONNECT it cannot verify is refused, and an authorisation check it cannot answer is denied.

ACL rows live in the same Postgres database as the rest of the provider's state — no separate auth store. When you unprovision a device, the provider deletes its mqtt_user_entity and mqtt_acl_entity rows in a single transaction.

CleanSession=true rationale​

Devices connect with CleanSession=true. The platform does not rely on the broker to queue messages for a device.

The provider's own sessions differ. The adapter session uses CleanSession=false, so the broker queues QoS 1 telemetry and reported properties while the provider is away and delivers them when it reconnects. The command session uses CleanSession=true: a command response is only useful while the call is still waiting for it.

Trade-offs for a device:

With CleanSession=true (recommended)With CleanSession=false
Broker drops queued messages on disconnectBroker queues messages until session expiry
State of record lives in Postgres + NATSState of record split between broker and platform
Subscriptions re-established on every reconnectSubscriptions persisted by broker
Predictable broker memory useBroker memory grows with offline device count

Device-side, the message-loss window during a reconnect is the price for keeping the broker stateless.

If you need durable offline message buffering for a specific device, the right answer is QoS-2 plus a session expiry on the device side — not a CleanSession=false default that affects the whole fleet.

Retained-state lifecycle​

The only message the provider publishes with retain=true is device/{deviceId}/twin/desired. This makes the latest desired-state snapshot available to the device immediately on (re)subscribe, without a round-trip to the platform.

   Operator updates desired property
│
▼
Provider publishes device/{id}/twin/desired (retain=true)
│
▼
Broker stores message under topic
│
▼
Device (re)subscribes → broker delivers retained message immediately
│
▼
Device applies desired-state, publishes twin/reported (retain=false)

If the device is not connected when the desired state changes, the provider marks the update pending and publishes it when the device connects.

When a device is unprovisioned, the provider clears the retained desired-state by publishing an empty payload (zero bytes) to the same topic with retain=true — the MQTT-spec idiom for clearing a retained message.

Telemetry, reported-state, and method messages are NEVER retained. Their semantics demand at-most-once delivery to fresh subscribers — replaying them would corrupt downstream pipelines.

Direct command invocation​

Commands are synchronous request-response calls from the platform to a device. The provider implements them as a paired publish:

Provider                                     Device
│ publish: │
│ device/{id}/command/{command}/invoke │
│ payload: { correlationId, payload } │
│ ─────────────────────────────────────────▶ │
│ │
│ device executes,
│ publishes response
│ │
│ received on the provider's wildcard │
│ subscription device/+/command/+/response/+│
│ topic: device/{id}/command/{command}/ │
│ response/{correlationId} │
│ ◀───────────────────────────────────────── │

The provider holds one wildcard subscription for all command responses, device/+/command/+/response/+. It takes the correlation ID from the last topic segment and completes the matching pending call; a response with an unknown or expired correlation ID is dropped. The provider publishes an invocation only when the device is connected. When the device does not answer within the wait (10 seconds on platform calls), the call fails; see Commands › Timeouts and Errors for what the API caller receives.

Credentials at rest​

Symmetric-key device credentials are protected by two distinct mechanisms, used at different points:

Encryption (provider-internal storage)​

The plaintext primary and secondary keys are encrypted with AES-256 using a 32-byte master key loaded from Credentials:MasterKey (base64) at startup. Each encryption call generates a fresh IV via RandomNumberGenerator, prepends it to the ciphertext, and base64-encodes the combined buffer.

Decrypt is the symmetric reverse: extract IV from the leading bytes, then decrypt the remainder.

┌─────────────────────────────────────────┐
│ Stored value (base64) │
│ ┌─────────┐ ┌──────────────────────┐ │
│ │ IV │ │ ciphertext │ │
│ │ (16 B) │ │ AES-256(plaintext) │ │
│ └─────────┘ └──────────────────────┘ │
└─────────────────────────────────────────┘

The provider returns plaintext credentials only over service-to-service paths (GET /devices/{id}/credentials, the bus-API get-credential query). They never leave Postgres in plaintext.

Hashing (Mosquitto-side authentication)​

Mosquitto's auth plugin compares device-supplied passwords against a stored bcrypt hash, NOT the AES ciphertext. When a device is provisioned, the provider computes:

mqtt_user_entity.password = bcrypt(plaintext_primary_key, workFactor: 10)

and stores the bcrypt hash. The AES-encrypted value is what onboarding retrieves to render device settings; the bcrypt hash is what the broker checks at CONNECT time. The two are written in the same provisioning transaction.

Rotating the master key requires re-encrypting every credential row but does NOT require re-hashing the bcrypt entries — those are independent.

Operational defaults​

ConcernDefaultNotes
MQTT version3.1.1 (v5 capable)Set per-device on the broker
Adapter QoS (subscribe)1At-least-once delivery, broker dedups by ClientId
Adapter QoS (publish)1At-least-once delivery to device
Master-key length32 bytes (256 bits)Validated at startup, fail-fast otherwise
Bcrypt work factor10~100 ms per CONNECT on modern hardware
Bus-API reply timeout5 sOnboarding treats expiry as provider_unavailable