MQTT Basics

Overview

Since MQTT is a pub-sub based protocol, server changes can be subscribed to, unlike HTTP where making an explicit request to fetch information is required.

One a connection to the MQTT server is established, the device uses to mechanism to Subscribe to events.

Subscribe

Getting information from the server is inherently different, where the device will subscribe to a (number) of topics and be notified immediately when new information becomes available:

client.subscribe(`v1/device/${DEVICE_ID}/commands`)

client.on('message', function (topic, message) {
    console.log(topic, JSON.parse(message.toString()))
})

Where with the HTTP approach, an explicit call needs to be done to the server to fetch a pending command. The challenge knowing when to make the call. This can be done via continuously sending Send Telemetry updates and checking the return value for the pending command flag.

fetch(`https://${SERVER_ENDPOINT}/v1/devices/${DEVICE_ID}/command`)

Since telemetries are only sent at certain intervals (5-100+) second. When a user sends a command to a device, it might take the device a long time to notice a command is pending and perform it. With MQTT, the device's subscription gets triggered almost immediately. Allowing for very fast user response time, without the downside of continuously polling the server.

Topics and Payloads

The topicsand payloadssupported by Xyte's implementation, match the HTTP APIs and as such detailed information on the structure of data sent and received by the device is available in the API Endpoints chapter.