Skip to main content
Version: v1 (Current)

Device Messaging and MQTT

MQTT is the device bus for provisioned readers, gateways and companion/device integrations. It is separate from an attendee plugin's runtime WebSocket subscription.

The current infrastructure includes cloud/gcp/main/modules/vernemq/ and broker callbacks under routes/webhook/v1/routes.php. VerneMQ callbacks use a distinct provisioned path token checked by app/Http/Middleware/AuthenticateVerneMqWebhook.php. That credential must not be distributed as an attendee app token or substituted for a project integration key.

For dashboard onboarding, use Readers and Device Provisioning. Reader Authentication documents the separate supported print-server API login; that endpoint is not an MQTT credential exchange or a general login for every device type.

Configure and operate​

Provision devices through the supported pairing/device flow, assign the intended project/access-point context, and verify active state, connection and synchronization. Inspect allowed topic/action behavior in the deployed broker authentication/authorization callbacks. A broker connection alone does not authorize publication to arbitrary projects.

Use a controlled device to test connect, allowed publish/subscribe, denied scope, disconnection and reconnection. Check the receiving platform resource and ingestion state separately from the broker's acknowledgment. Offline reader snapshots and MQTT transport are different mechanisms.

Callback outcomes and heartbeat behavior​

The VerneMQ webhook middleware reads its distinct configured path token and compares it with hash_equals. An unset configured token or mismatch returns HTTP 401. This authenticates the broker callback; device/topic authorization is a separate decision.

Publish and subscribe ACL refusals return HTTP 200 with result.error set to not_allowed. Inspect the JSON decision rather than treating HTTP success as permission to publish. A mixed subscription request is evaluated per topic: allowed filters retain their requested QoS, while refused filters receive failure codes (128 for MQTT 3.1.1 or 135 for MQTT 5 hooks). If none are allowed, the callback returns the denied result.

An allowed publish to the client's own configured-prefix/devices/<clientId>/status topic updates last_seen_at for its live reader identifier and is deliberately uncached. Other allowed publishes can carry Cache-Control max-age from publish_cache_ttl (default 60 seconds). Broker decision caching and application heartbeats therefore have different freshness behavior; a successful scan publish is not automatically the same signal as a status heartbeat.

The callbacks delegate actual device/topic rules to MqttAclResolver. Do not infer permitted topic patterns from a successful connection or substitute a broad wildcard subscription. Verify the resolver and current provisioning state for the intended client before deployment. Detailed broker configuration and secret rotation remain in private operator runbooks.

Callback payload validation​

Publish authorization requires string client_id and topic. username, integer qos, boolean retain and mountpoint are optional. The broker's publish hook omits the message payload; authorization here is based on device/topic context, not scan or command-body validation. Downstream ingestion must validate the actual message content.

Subscribe authorization requires client_id and a nonempty topics array; each entry needs a string topic and integer qos. For example, a callback for an unclaimed device using the default prefix can describe its own pairing subscription as:

{"client_id":"DEVICE_IDENTIFIER","topics":[{"topic":"gxp/devices/DEVICE_IDENTIFIER/pairing","qos":1}]}

This is a broker callback body, not a public device API or credential exchange. The broker still supplies its callback credential through the protected route. Use the client's real identifier consistently and preserve the broker's protocol-specific hook header. Request rules type-check QoS but do not impose a 0–2 range themselves; broker protocol validation and callback shape validation are separate layers.

Device topic ACLs​

For ordinary device identities, the resolver uses the configured prefix (default gxp) and the client's own identifier. “Own pairing/status” means gxp/devices/CLIENT_ID/pairing and gxp/devices/CLIENT_ID/status with that default prefix; “own subtree” begins gxp/devices/CLIENT_ID/. Replace CLIENT_ID with the exact provisioned reader identifier, not its numeric database ID. Keep the prefix consistent with the deployment. A revoked reader is denied all actions. Unknown action names are rejected. Cross-device topics under the device prefix are denied for ordinary devices.

Device state/pathSubscribePublish
Unclaimed deviceOwn pairing topicOwn status topic
Unclaimed BLE gatewayAlso its lower-case /gw/<clientId>/actionAlso its lower-case /gw/<clientId>/response; not the fleet scan topic
Paired/provisioned ordinary deviceOwn device-topic subtreeOwn subtree except exact command and provision topics
Paired/provisioned BLE gatewayAlso own gateway action topicAlso own gateway response and configured fleet scan topic

The resolver recognizes separate service identities for bridge subscriptions, backend HTTP publishing and the internal gateway client before ordinary device-topic rules. Keep those identities in their provisioned server components. Their grants do not describe an attendee app or normal device credential, and a topic ACL decision does not authenticate the submitted username/client ID by itself; the broker registration path must do that.

A missing reader is treated as unclaimed by the ordinary-device branch. The ACL resolver does not independently enforce every active-state or credential rule. Verify registration, reader resolution and provisioning along with topic authorization before calling a device fully authorized. Do not infer that a permitted own-topic subscription grants all broker wildcards or cross-device access.

Registration and device reuse​

The register callback resolves the newest live reader for client_id. Here live means not archived in the model scope; it does not mean currently connected or active. It accepts configured bridge/backend credential pairs, the configured internal client identity plus credentials, bootstrap credentials for unclaimed clients, or a reader's hashed per-device access code. Missing configured credential values do not match. Broker login denial uses the same HTTP-200 not_allowed result contract as topic denial.

Bootstrap credentials also support device reuse. For a revoked reader, or a paired/provisioned reader presenting valid bootstrap credentials after reset, registration calls archiveAndRecreate and allows the connection. This changes pairing state; it is not merely a password retry. Protect the shared bootstrap credential and use the supported re-pair workflow when moving a device between projects.

Per-device access-code verification checks the stored hash. This check alone is not a statement that every active/project constraint has been evaluated; combine the registration path with reader resolution, pairing/provisioning and ACL rules. Keep server bridge credentials separate from device access codes and the broker callback's path token.

Do not test registration using a real claimed device identifier and bootstrap credentials just to probe connectivity, because that path can recreate the reader claim. Use a controlled fixture/device and inspect pairing state as part of acceptance. Detailed provisioning and credential rotation procedures remain in private operator documentation.

Connection lifecycle and asynchronous work​

After broker registration, the connected hook dispatches ProcessDeviceConnectedJob for a nonempty client ID unless it recognizes the configured internal client ID or bridge username. The job delegates to DevicePairingService::onDeviceConnected. The hook immediately returns an empty JSON object; that acknowledgment does not prove the job ran, a pairing code was published or the device received it.

Offline and gone notifications share the offline handler. For a supplied identifier it updates live matching readers' device_sync_status to OFFLINE and returns an empty object. It does not revoke the credential, remove the project assignment or cancel already queued work. Reconnection, heartbeat freshness, pairing state and synchronization status are separate signals.

If a device connects but no pairing progress appears, inspect the connected job and pairing result as well as the broker callback. If a device displays offline, compare the offline event with later heartbeat/connection processing instead of immediately re-pairing it. Test duplicate/reordered lifecycle events with a controlled device before assuming status updates represent an ordered delivery history.

Pairing code refresh on connection​

The connected job finds or creates the live reader. New/unclaimed readers receive a fresh pairing code and configured expiry (default 600 seconds), so reconnecting can replace the code a user was about to enter. It merges supplied device information and updates last_connected_at before publishing the pairing payload.

The pairing publication contains type: pairing_code, pairing_code and expires_at fields. Consume it through the supported device client; this callback guide does not define every status/claim/provision payload or acknowledgment protocol for a new client. Verify the supported client protocol before implementing a custom device.

The stored reader update precedes MQTT publication, and this path does not use the publish boolean to roll back the saved code. A code visible in the dashboard or a completed connected job is therefore not proof the device received that code. Inspect the latest connection/code and publish outcome together when diagnosing stale screens.

For eligible paired/provisioned non-gateway readers with a claim-republish flag, the service consumes that flag and can republish a recoverable claim payload. If the encrypted access code cannot be recovered, it logs a warning and returns from that republish path. Do not assume every reconnect creates a new claim or repairs every missed provisioning message.

Backend publish and connection diagnostics​

VerneMqClient uses separate HTTP publish and management listeners. Its publish method JSON-encodes array payloads, defaults to QoS 1 without retention, and returns false for encoding failure, a connection exception or a non-success HTTP response. A true return means the broker HTTP request succeeded; it does not prove device receipt or command execution. Preserve the command/result correlation used by the owning integration.

Single-client lookup returns null for an absent session, unreachable management API or unsuccessful response. isClientConnected therefore cannot distinguish every transport failure from an offline device. The cluster-list method makes a useful distinction: null means unavailable/invalid management response, while an empty array means no online clients were found in a successful result. It filters online rows and applies a requested limit (default 10,000), so it is not proof of an unlimited fleet inventory.

A revoke path can request a forced disconnect with session cleanup. The client returns the management HTTP outcome; confirm actual disconnection and any device reconnection separately. Do not interpret a failed management call as a completed revocation or assume queued messages were removed without verifying the broker outcome.

Earlier EMQX notes​

EMQX architecture and rollout notes (repository reference: ./emqx-mqtt.md) describe an earlier implementation/roadmap. Do not use them as a deployment guide for the current VerneMQ modules, callback authentication or topic configuration. Compare the deployed broker and current infrastructure before changing an operational listener.

Troubleshooting​

Start with pairing/credential validity, project/point ownership, device active state, topic/action authorization and actual broker callback results. For gateway ingestion, inspect downstream processing as well. Avoid copying production device tokens into support material.