Skip to main content

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

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 the status 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.
This is more reliable than heartbeat-based detection alone, since the broker publishes the LWT within seconds of losing the TCP connection.

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.
The idempotency dedup layer on the device (LRU cache of recent commandId values) ensures that redelivered QoS 1 messages do not cause duplicate command execution.

Authentication

MQTT authentication uses the username and password fields of the MQTT CONNECT packet: The API key is also sent in a separate non-retained status message after connection for server-side validation. Retained status messages never contain the API key.

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