Skip to main content

MQTT

MQTT is the most broadly interoperable MAGPIE transport: Python, C++, Node.js, and browsers can all participate. Because clients connect outbound to a broker, MQTT also works naturally for robots and services behind NAT.

URI schemes​

URIConnection
mqtt://host:1883Plain MQTT over TCP
mqtts://host:8883MQTT over TLS
ws://host:port/mqttMQTT over WebSocket
wss://host:port/mqttMQTT over secure WebSocket

Browsers require ws:// or wss://. Use TLS (mqtts:// or wss://) outside a trusted development network.

Share one connection​

Create one connection per process or logical broker identity, connect it, and pass it to multiple stream and RPC components.

from luxai.magpie.transport import (
MqttConnection,
MqttStreamReader,
MqttStreamWriter,
MqttRpcRequester,
)

connection = MqttConnection("mqtts://broker.example.com:8883")
connection.connect()

writer = MqttStreamWriter(connection)
reader = MqttStreamReader(connection, topic="robot/+/status")
client = MqttRpcRequester(connection, service_name="robot-01/actions")

Authentication, TLS, LWT, and QoS​

from luxai.magpie.transport import (
MqttAuthOptions, MqttConnection, MqttDefaultsOptions,
MqttOptions, MqttTlsOptions, MqttWillOptions,
)

options = MqttOptions(
auth=MqttAuthOptions(
mode="username_password", username="robot-01", password="secret"
),
tls=MqttTlsOptions(ca_file="/etc/ssl/certs/broker-ca.pem", verify_peer=True),
will=MqttWillOptions(
enabled=True, topic="robot/robot-01/status", payload="offline", qos=1, retain=True
),
defaults=MqttDefaultsOptions(publish_qos=1, subscribe_qos=1),
)

connection = MqttConnection(
"mqtts://broker.example.com:8883", client_id="robot-01", options=options
)

Production guidance​

  • Give every process a unique client ID.
  • Use broker ACLs to restrict topics by robot, tenant, and role.
  • Never place credentials directly in source code; load secrets through your deployment environment.
  • Use LWT for presence, but treat it as an operational signal rather than proof of device health.
  • Select QoS based on message meaning and measure the cost on constrained networks.
  • Namespace development, staging, and production topics separately.