Skip to main content

What is MQTT?

MQTT (Message Queuing Telemetry Transport) is a lightweight, publish-subscribe messaging protocol designed for constrained devices and low-bandwidth, high-latency, or unreliable networks. It’s ideal for Internet of Things (IoT), M2M (machine-to-machine), and mobile applications.

Key Characteristics

Lightweight

Minimal protocol overhead with small packet sizes, perfect for constrained devices and networks.

Publish-Subscribe

Decoupled communication model where publishers and subscribers don’t need to know about each other.

Quality of Service

Three QoS levels (0, 1, 2) to guarantee message delivery based on your needs.

Persistent Sessions

Maintain connection state and queued messages even when clients disconnect.

Publish-Subscribe Model

MQTT uses a broker-based architecture where clients connect to a central broker (HiveMQ CE) that routes messages between publishers and subscribers.

Topics

Messages are published to named topics using a hierarchical structure with forward slashes as delimiters:
home/livingroom/temperature
home/bedroom/humidity
factory/machine1/status
factory/machine2/telemetry

Wildcards

Subscribers can use wildcards to subscribe to multiple topics:
  • Single-level wildcard (+): Matches one level
    home/+/temperature  # Matches home/livingroom/temperature, home/bedroom/temperature
    
  • Multi-level wildcard (#): Matches multiple levels
    home/#  # Matches home/livingroom/temperature, home/bedroom/humidity, etc.
    

HiveMQ CE MQTT Support

HiveMQ Community Edition provides complete implementation of MQTT protocols:
Full support for MQTT 3.1.1, the most widely adopted version:
  • All QoS levels (0, 1, 2)
  • Retained messages
  • Last Will and Testament (LWT)
  • Clean/persistent sessions
  • Username/password authentication
  • TLS/SSL encryption

Protocol Versions

VersionYearStatusKey Features
MQTT 3.12010SupportedOriginal standardized version
MQTT 3.1.12014Widely UsedRefinements and clarifications
MQTT 5.02019LatestEnhanced features, better error handling
HiveMQ CE automatically detects the MQTT version from the client’s CONNECT packet and responds accordingly.

When to Use MQTT

MQTT is ideal for:
  • IoT Devices: Sensors, actuators, smart home devices with limited resources
  • Mobile Applications: Push notifications, location tracking, chat applications
  • Industrial IoT: Factory automation, machine telemetry, SCADA systems
  • Smart Cities: Traffic monitoring, environmental sensors, public transportation
  • Healthcare: Patient monitoring, medical device connectivity
  • Automotive: Telematics, vehicle-to-vehicle (V2V) communication

MQTT vs Other Protocols

FeatureMQTTHTTP/RESTWebSocket
PatternPub/SubRequest/ResponseBidirectional
OverheadVery LowMediumLow
Real-timeExcellentPoorExcellent
QoSBuilt-inManualManual
BandwidthMinimalHigherLow
Best ForIoT, M2MWeb APIsWeb Real-time
HiveMQ CE supports MQTT over WebSocket, combining MQTT’s efficiency with WebSocket’s browser compatibility.

Basic MQTT Workflow

1

Connect

Client establishes a connection to the MQTT broker (HiveMQ CE) using TCP, TLS, WebSocket, or Secure WebSocket.
import paho.mqtt.client as mqtt

client = mqtt.Client()
client.connect("broker.example.com", 1883, 60)
2

Subscribe

Client subscribes to one or more topics to receive messages.
client.subscribe("home/+/temperature")
3

Publish

Publisher sends messages to topics.
client.publish("home/livingroom/temperature", "22.5")
4

Receive

Subscribers receive messages from their subscribed topics.
def on_message(client, userdata, message):
    print(f"{message.topic}: {message.payload.decode()}")

client.on_message = on_message

Next Steps

MQTT 3.1.1

Learn about MQTT 3.1 and 3.1.1 features

MQTT 5

Explore MQTT 5.0 enhancements

Transport Protocols

Configure TCP, TLS, and WebSocket transports

Connecting Clients

Connect MQTT clients to HiveMQ CE

Pub/Sub Guide

Learn publishing and subscribing patterns

Quality of Service

Understand QoS levels

Additional Resources

Build docs developers (and LLMs) love