MQTT Protocol

MQTT, which stands for MQ Telemetry Transport, is a lightweight communication protocol that targets low-power devices with limited connectivity. MQTT is a mechanism that allows for:

MQTT Clients

Losant provides MQTT clients that easily wrap up the communication between Losant and a device for the following languages:

If you are not working in one of the languages above, you can use the Losant MQTT Broker directly with any MQTT client. See below for more information.

Learning MQTT

The core concept of MQTT involves publishing and subscribing to topics. Clients can publish any data they choose to any topics they choose. Other clients can then subscribe to those topics to receive that data.

What facilitates this communication is a central service called a message broker. All clients open a connection to the message broker and the broker is responsible for properly routing messages to subscribers.

The Losant Message Broker

In order to support the existing MQTT implementations, Losant provides an MQTT message broker that can be used for any arbitrary topics and payloads. To make use of Losant features including data collection, visualization, and workflows, Losant provides an opinionated MQTT implementation that must be followed.

The Losant Message Broker can be reached using several transports:

  • TCP—mqtt://broker.losant.com:1883
  • TLS—mqtts://broker.losant.com:8883
  • WebSockets—ws://broker.losant.com:80
  • Secure WebSockets—wss://broker.losant.com:443

Broker Authentication

Losant requires the client ID, username, and password fields be correctly set on all MQTT connect calls:

  • client id—Must be set to a valid Device ID that is already registered with the Losant Platform.
  • username—Must be set to a Losant Access Key.
  • password—Must be set to a Losant Access Secret, which is generated when creating an Access key.

For example, a connect call using the JavaScript MQTT client is displayed below:

var client = mqtt.connect('mqtts://broker.losant.com', {
  clientId: 'my-device-id',
  username: 'my-access-key',
  password: 'my-access-secret',
})

Access Control

To connect your devices to the Losant MQTT Broker, you must use Access Keys. By default, access keys only allow access to the device-specific topics (e.g. state and commands for every device you have allowed.

For additional control, see Additional MQTT Topics Access.

Message Limits

Losant’s MQTT broker usage is subject to a few limits:

  • 30 messages in a 15-second window per topic (or, on average, two per second).
  • 300 messages in a 15-second window per client connection (or, on average, 20 per second).
  • 300 new client connections in a 15-second window per application (or, on average, 20 per second).
  • 256KB per message.

Note: In this context, a “client connection” is any type of device that has authenticated against and is connected to the Losant MQTT broker.

Limits are applied individually per direction. For example, a device may receive two messages per second and publish two messages per second on the same topic without exceeding the per-topic rate limit.

If a device exceeds either of the message rate limits, it will be banned for 30 seconds from the broker. If the device reconnects and exceeds the rate limits again within 15 minutes, the ban time will double. This ban time will continue to double after each violation until a maximum ban time of 1 hour is reached. If at any point the device, after connecting, does not violate message limits for 15 minutes, the ban time for the next violation resets to 30 seconds.

Root CA Certificate

If your device fails to connect over a secure channel (via mqtts or wss protocols), you may need to install install the ‘DigiCert Global Root CA’ certificate on your device. This is the certificate authority that Losant uses for secure connections to our MQTT broker.

MQTT Version and Limitations

Losant supports MQTT version v3.1.1 with the following exceptions:

  • Clients may only subscribe to topics using QoS 0. Clients may publish messages to the broker using QoS 0 or QoS 1.
  • Retained messages are not supported.
  • CleanSession 0 is not supported.
  • Maximum message payload size is 256KB.

Losant MQTT Topics

A Losant topic is anything that starts with losant. Messages published to Losant topics gain access to the full features of the Losant IoT Platform, including data collection, visualization, and workflows.

For Losant to properly parse and understand these messages, a defined JSON-based payload format must be followed.

Publishing Device State

Device State is likely the most commonly published message. When thinking in terms of sensor data, the device state is typically the value of one or more sensors.

State Topic Form

losant/DEVICE_ID/state

State Payload Form

The payload (message body) of a state report is a JSON object that takes the following shape:

{
  "data" : {
    "aStringAttribute": "The attribute value",
    "aNumberAttribute": 42
  },
  "time": "2022-10-27T12:34:56.789Z",
  "flowVersion": "ver 1.0",
  "meta": {
    "firstBoot": true
  }
}
  • data (Required): An object whose keys are device attribute names, and the value of each key is the telemetry data to record for that attribute.

    • Any attribute (object key) that does not have an attribute of the same name configured on the device will be ignored.
    • Each value’s data type must match the attribute data type as it is configured on the device. If the provided value cannot be parsed to match the attribute’s data type, that value will be ignored and will not be written to the time series database.
  • time (Optional): The timestamp at which to record the state report’s attribute values. When not included, this defaults to the current time. The value can be sent in several time formats, and the inclusion of a timestamp can be beneficial when batch reporting data, overwriting previously recorded data, or reporting historical data.
  • flowVersion (Optional): Specifies which workflow version to run when triggered by the payload. For each workflow containing a Device: State Trigger targeting the reporting device …

    • If flowVersion is included, any Application Workflow with a version matching the provided name will fire - assuming the version has a Device: State Trigger targeting the reporting device. If no version of a workflow matching the provided flowVersion is found, no version of that workflow will fire.
    • If flowVersion is not included, the default version of any Application Workflow will fire if that default version includes a Device: State Trigger targeting the reporting device.
  • meta (Optional): Additional information to expose on the initial payload of any Device: State Triggers that fire because of the state report. This may take any shape – string, object, number, etc. The value is not recorded with the state report; it is only available in these workflow payloads and cannot be retrieved later.

Publish State Example

You’ll generally have an attribute for each sensor attached to your device. For example, if a device with ID 575ecf887ae143cd83dc4aa2 has a temperature sensor, you might report a state that has an attribute named “temperature” by publishing to the topic below with the following payload:

losant/575ecf887ae143cd83dc4aa2/state
{
  "data": {
    "temperature": 72
  },
  "time": { "$date": "2016-11-04T19:42:06.710Z" }
}

When a device publishes data in this format, Losant automatically stores the data and makes it available in our Dashboards as well as exposing it through Workflows. The attributes you send must be configured on the device before Losant will accept the data.

Subscribing to Commands

Device Commands instruct your device to perform a specific action. Commands are typically initiated using Losant Workflows. Commands include a name and an optional payload.

Command Topic Form

losant/DEVICE_ID/command

Payload Topic Form

{
  "name": "command-name",
  "payload": {}
}

name—Command name.

payload—Any JSON value that provides necessary arguments to your command.

Commands do not have to be pre-registered with Losant for them to be received. What commands your device supports is entirely up to your specific application and your device’s firmware.

Example Command Subscription

Below is a command example that instructs a thermostat associated with the device ID 575ecf887ae143cd83dc4aa2 to set itself to a specific temperature. The following payload is published on the topic below, and the device is listening on that topic for command messages:

losant/575ecf887ae143cd83dc4aa2/command
{
  "name": "set-temperature",
  "payload": {
    "temperature": 72
  }
}

Custom Topics

The Losant Broker also supports custom topics. Custom topics must be valid MQTT topics, and they cannot be an MQTT system topic (topics that start with “$SYS”) or a Losant device-specific topic. Further, they cannot be topics that begin with ’$‘. For example, $myTopic/subtopic is not an accepted custom topic.

Example:

alert/machine

Custom topics can trigger workflows with the MQTT Trigger Node, and you can publish to custom topics using the MQTT Output Node.

Custom Topic Access Control

By default, access keys only allow access to the Losant device-specific topics (e.g. state and commands) for every device you have allowed. Optionally, you can give access to all custom topics or specific custom topics. See Additional MQTT Topics Access for more information.

Wildcard Topics

The Losant Broker supports wildcard topics. Both single-level and multi-level wildcards are supported in Application Workflows:

Single-level: custom/+/temperature

Multi-level: custom/#

Gateway Edge Agent MQTT Broker

The Gateway Edge Agent exposes a local MQTT broker for local machine-to-machine (M2M) communication, or to trigger Edge Workflows using the MQTT Trigger, which can then process and forward sensor data to the cloud.

You may learn more in the Gateway Edge Agent broker documentation.

Was this page helpful?


Still looking for help? You can also search the Losant Forums or submit your question there.