An overview of the MQTT publish subscription pattern

MQTT is based on Publish/Subscribe mode for communication and data exchange, which is fundamentally different from the HTTP Request/Response mode.

A Subscriber subscribes to a Topic from the message Broker. After a successful subscription, the message server forwards messages for that topic to all subscribers.

Topics are separated by a ‘/’ to distinguish between different levels. Topics that contain the wildcard ‘+’ or ‘#’ are called Topic Filters, and topics that do not contain the wildcard are called Topic Names. Such as:

sensor/1/temperature

sensor/1/#

sensor/+/temperature
Copy the code

MQTT Broker profile

MQTT Broker definition and its role

An MQTT Broker, also known as an MQTT message server, can be a server or a cluster of servers running MQTT message server software. An MQTT Broker receives network connections from clients and processes Subscribe/Unsubscribe, Publish requests from clients, as well as forwarding messages published by clients to other subscribers.

MQTT Broker is widely used in: power, new energy, smart city, smart home, smart meter reading, Internet of vehicles, finance and payment, operators and other industries.

Common open source MQTT brokers

  • EMQ X – EMQ X is based on the Erlang/OTP platform and is the most popular MQTT messaging server in the open source community. In addition to THE MQTT protocol, EMQ X also supports MQTT-SN, CoAP, LwM2M, STOMP and other protocols. At present, EMQ X has 5000+ enterprise users and 20+ fortune 500 partners in the global market.
  • Eclipse MosquittoMosquitos – Mosquitos is an early open source MQTT Broker that contains mosquitos a C/C ++ client library, along with mosquitos for publish and subscribemosquitto_pub,mosquitto_subCommand line client. Mosquitos are lightweight and suitable for use on everything from low-power single-board computers to full servers.
  • VerneMQ – Based on the Erlang/OTP platform, VerneMQ is a high-performance distributed MQTT message broker. It can scale horizontally and vertically on hardware to support a large number of concurrent clients while maintaining low latency and fault tolerance.
  • HiveMQ CE – HiveMQ CE is an open source Java-based MQTT messaging server that fully supports MQTT 3.x and MQTT 5 and is the foundation of the HiveMQ Enterprise Edition messaging connection platform.

The main functions implemented by MQTT Broker

Protocol access

  • Complete MQTT V3.1/V3.1.1 and V5.0 protocol specification support;
  • Mqtt-sn, CoAP, lwM2M and other iot protocol access support.

Cluster deployment

Multi-server node cluster, and supports automatic node discovery. Clustering brings the following advantages through collaboration among multiple servers over a single server:

  • High availability. The failure of a single or small number of servers does not interrupt the entire messaging service, and the remaining functioning nodes can continue to provide service.
  • Load balancing. The load balancing mechanism enables a cluster to evenly distribute load among nodes.
  • Higher overall performance. Compared with single-node deployment, multi-node cluster can exponentially improve the connection and message processing capability of the entire system.
  • Scalability. Capacity expansion can be accomplished by adding new nodes to the cluster without downtime.

Access security

  • SSL, WSS encrypted connection, and single/two-way security authentication support;
  • Client ID, IP address, user name and password, LDAP, and browser Cookie authentication are supported.
  • Access control based on client ID, IP address, and user name;
  • Message rate and connection rate limits.

Data persistence

The main application scenarios of data persistence include recording operations such as client online and offline status, subscription topic information, message content, and sending message receipt upon message arrival to Redis, MySQL, PostgreSQL, MongoDB, Cassandra and other databases.

Other features

  • HTTP message publishing interface support, so that the upper-layer applications can more conveniently send messages to the device through THE REST API;

  • MQTT Broker bridging, which enables the bridging of messages between different MQTT brokers or between different clusters. Bridging makes it easy to bridge messages to cloud services, streaming services, or other MQTT messaging servers. Bridges can provide some functions that cannot be provided by clusters alone: cross-VPC deployment, support heterogeneous nodes, and increase the service upper limit of a single application.

  • Shared subscriptions are supported. A shared subscription is a mechanism that allows message distribution from a subscription group to be distributed evenly to the members of the subscription group. In a shared subscription, clients subscribing to the same topic receive messages under that topic in turn. The same message will not be sent to multiple subscription clients so as to achieve load balancing among multiple subscription clients.

  • Rules engine support for configuring processing and response rules for message flows and device events. Rules describe three configurations: where data comes from, how data is filtered and processed, and where the results go. That is, an available rule contains three elements: Trigger events (triggered when a condition is met), processing rules (filtering and processing data from context information), response actions (such as persisting to a database, republishing processed messages, forwarding messages to message queues, and so on).

Use of MQTT brokers

For testing purposes, we used an online version of the Broker provided by EMQ that includes all the capabilities of EMQ X Enterprise.

Broker address: broker.emqx.io

Broker ports: 1883, 8883 (SSL), 8083 (Websocket), 8084 (WSS)

To connect to the client we use the online version of Websocket provided by EMQ: tools.emqx.io.

Connections to MQTT brokers

Use your browser to open the address Tools.emqx. IO, click on the New Connection button in the lower left corner, and fill in the link information in the box on the right. After completing the required fields, click the Connect button to create a link and Connect to the Broker.

News release

After the connection is successful, click Write a Message in the lower right corner to pop up the message publishing box. Fill in Topic and Payload and click the send icon to publish the message.

Subject to subscribe to

  • Subscribe to general topics

    In the intermediate Subscriptions module, subscribe to the Hello theme. If you send a message to the Hello topic, the message will be received in the message list (the received message is shown on the left).

  • Subscribe to wildcard topics

    Subscribe to the wildcard topic/# and send a message to the TestTopic /1 topic, which will be received in the message list.


For more information, please visit our official website emqx. IO, or follow our open source project github.com/emqx/emqx. For more details, please visit our official documentation.