Skip to content

MQTT 5.0 Bindings

MQTT (Message Queuing Telemetry Transport) is a lightweight, publish-subscribe network protocol that transports messages between devices. It is an OASIS standard and is ideal for connecting remote devices with a small code footprint and minimal network bandwidth. AsyncAPI provides comprehensive bindings for MQTT 5.0, the latest version of the protocol, allowing you to define your event-driven APIs with precision.

What is MQTT 5.0?

MQTT 5.0 is a significant upgrade over previous versions, introducing features that enhance scalability, reliability, and security:

  • Enhanced Authentication: Support for challenge/response authentication.
  • Session Expiry: Clients can maintain session state for a defined period, even when disconnected.
  • Message Expiry: Messages can be assigned an expiry interval.
  • Shared Subscriptions: Built-in load balancing for consumers.
  • Reason Codes: Detailed feedback for all operations (e.g., CONNACK, PUBACK).
  • Custom User Properties: Add user-defined metadata to almost any MQTT packet.
  • Flow Control: Request/response-based flow control to prevent overwhelming clients.

AsyncAPI MQTT 5.0 Bindings Overview

AsyncAPI MQTT 5.0 bindings define how your API specification maps to MQTT 5.0 concepts:

Binding Types

Binding TypePurposeDescription
Channel BindingDefine channel configurationsSpecifies how channels map to MQTT topics
Operation BindingConfigure message operationsDefines Quality of Service (QoS) and other publish/subscribe options
Message BindingMessage-level configurationsDefines message properties like expiry and content type
Server BindingServer-level configurationsDefines session parameters like Session Expiry Interval

Supported Versions

VersionStatusKey Features
0.2.0LatestSupport for MQTT 5.0 features, including sessionExpiryInterval.
0.1.0LegacyBasic MQTT support.

Key MQTT 5.0 Concepts

Topics

MQTT uses topics to filter messages for clients:

  • Topic-based addressing: Messages are published to topics (e.g., home/livingroom/temperature).
  • Hierarchical structure: Slash-separated topic levels create a topic tree.
  • Wildcard subscriptions: + for a single level, # for multiple levels.

Quality of Service (QoS)

MQTT provides three levels of Quality of Service for message delivery:

  • QoS 0 (At most once): Fire-and-forget, no acknowledgment.
  • QoS 1 (At least once): Message is guaranteed to be delivered, but duplicates may occur.
  • QoS 2 (Exactly once): Message is guaranteed to be delivered exactly once.

Retained Messages

A retained message is a standard MQTT message with the retained flag set to true. The broker stores the last retained message and the corresponding QoS for a specific topic. New subscribers to that topic receive the last retained message immediately after subscribing.

Last Will and Testament (LWT)

The Last Will and Testament feature allows a client to notify other clients about an ungraceful disconnection. It's a message that the broker will publish on a specified topic on behalf of the client if the client disconnects unexpectedly.

Use Cases

MQTT 5.0 bindings are ideal for:

Internet of Things (IoT)

  • Device Telemetry: Efficiently send sensor data from thousands of devices.
  • Smart Home: Control lights, thermostats, and other home automation devices.
  • Industrial IoT (IIoT): Monitor and control industrial machinery and processes.
  • Connected Vehicles: Send and receive data from cars for telematics and remote control.

Mobile Applications

  • Push Notifications: Deliver reliable notifications to mobile apps.
  • Chat Applications: Build lightweight and efficient real-time chat services.
  • Location Tracking: Track assets or personnel in real-time.

Event-Driven Architecture

  • Microservices Communication: Decouple services with a lightweight message bus.
  • Real-time Data Distribution: Broadcast real-time updates to multiple clients.
  • Status and Monitoring: Distribute health checks and status updates from services.

Getting Started

Basic Server Configuration

yaml
servers:
  productionBroker:
    url: mqtts://api.example.com
    protocol: mqtt
    bindings:
      mqtt5:
        bindingVersion: '0.2.0'
        sessionExpiryInterval: 300

Basic Channel and Operation

yaml
channels:
  user-updates:
    address: 'user/{userId}/update'
    bindings:
      mqtt5:
        bindingVersion: '0.2.0'
    messages:
      userUpdate:
        $ref: '#/components/messages/UserUpdate'

operations:
  receiveUserUpdate:
    action: receive
    channel:
      $ref: '#/channels/user-updates'
    bindings:
      mqtt5:
        bindingVersion: '0.2.0'

MQTT vs Other Technologies

MQTT vs HTTP

  • MQTT: State-aware, publish-subscribe, efficient for small data.
  • HTTP: Stateless, request-response, more overhead per message.

MQTT vs CoAP

  • MQTT: Runs over TCP, reliable transport, centralized broker.
  • CoAP: Runs over UDP, designed for constrained networks, can be peer-to-peer.

MQTT vs NATS

  • MQTT: Dominant in IoT, client-centric features (LWT, retained messages).
  • NATS: Higher performance, focused on cloud-native backend systems.

Best Practices

Topic Naming

  • Use a hierarchical structure that is specific and extensible.
  • Avoid leading or trailing slashes.
  • Don't use spaces or non-ASCII characters.

Quality of Service

  • Use QoS 0 for non-critical telemetry data.
  • Use QoS 1 for commands or important notifications where duplicates can be handled.
  • Use QoS 2 for critical operations like billing or financial transactions.

Security

  • Always use TLS to encrypt communication.
  • Implement robust authentication and authorization on the broker.
  • Use the principle of least privilege for topic access.

Binding Documentation

Channel Bindings

Operation Bindings

Message Bindings

Server Bindings