When to Use MQTT
Use MQTT instead of the default Socket.IO transport when:- Your device has limited RAM (microcontrollers, Pico W)
- The network connection is unreliable or intermittent
- You need offline message queueing (commands delivered when the device reconnects)
- You are already running an MQTT infrastructure
- The device is battery-powered and needs a lightweight protocol
The Lua Device Gateway runs an MQTT broker at
wss://mqtt.heylua.ai/mqtt. You do not need to run your own broker.Configuration
For a new Node.js installation, provision a device credential for the exact agent, device name, and operations that the device uses. The published Python 1.3.0 client and the current MicroPython distribution continue to useapi_key with existing legacy keys.
Node.js
Python
MicroPython
Topic Structure
All MQTT topics follow a consistent prefix pattern:Last Will and Testament (LWT)
The MQTT client automatically sets a Last Will and Testament message on thestatus topic. If the device disconnects unexpectedly (network failure, power loss), the broker publishes the LWT message, which the gateway uses to immediately mark the device as offline.
QoS Levels
The device client uses QoS 1 for all messages except heartbeats. Combined with persistent sessions (
clean: false), this means commands are queued by the broker when the device is temporarily offline and delivered when it reconnects.
Authentication
MQTT authentication uses theusername and password fields of the MQTT CONNECT packet:
For a typed device credential, the CONNECT password is the only MQTT message that contains the secret. The client sends its command manifest in a separate non-retained status message without the credential. Retained status messages never contain a secret.
Existing clients that use a non-dotted legacy key keep their current behavior. They send the key as the CONNECT password and in the existing non-retained
apiKey status field. See Device credentials.
Complete Example
A humidity and temperature monitor that fires an alert trigger when conditions are out of range:Next Steps
MicroPython Client
Run on a Raspberry Pi Pico W with native MQTT
Pico W Setup Guide
Step-by-step hardware setup with Thonny
Node.js Client
Socket.IO transport for full-featured Node.js devices
Architecture
Understand the full transport comparison

