MQTT is based on a publish/subscribe model for message communication. The client can be both a publisher (publish a message) and a subscriber (subscribe to a topic). That is, when a client publishes a message to the broker, the broker will forward the message to clients subscribed to this topic after receiving it.
This chapter will introduce the commonly used MQTT client tools, and will walk you through the process of establishing client connections, subscribing to topics, publishing messages, as well as receiving and viewing messages.
Messaging workflow
Below is the typical workflow on how a message is published and then received by the subscriber.
Publishers and subscribers initiate the connection request to EMQX with the correct connection URL and authentication information. Note: Both are connected to the same EMQX;
The subscriber subscribes to the topic. By default, messages with no matching subscribers will be discarded immediately;
The publisher publishes messages, please ensure that the topic of the message can be matched with the subscriber, and the subscriber has subscribed to the corresponding topic;
The subscriber receives the message.
In the next section, we will introduce how to test the connection with different client tools, including Dashboard Websocket and MQTT X.
MQTT X(opens new window) is a cross-platform MQTT desktop client open sourced by EMQ(opens new window). It can run on macOS, Linux, Windows, and Ubuntu. MQTT X has provided features like support for customized scripts for MQTT Pub/Sub simulation, format conversion for MQTT messages, and logging.
Leveraging the easy-to-use graphic interface of MQTT X, you can connect MQTT broker or cloud with one click and test MQTT/TCP, MQTT/TLS, and MQTT/WebSocket connections.
You can also use MQTT X Web(opens new window) to complete the development and debugging via MQTT over WebSocket, with no need to download or install any tools.
Below is the workflow of how to use MQTT X to connect, publish/subscribe, and view messages:
Configure the MQTT connection and message publish/subscribe services: Fill in the EMQX connection information and establish a connection. For detailed operations, see MQTT X - Get Started(opens new window). For Host, select the mqtt:// protocol and fill in the IP address (Default: localhost) of the corresponding listener, set Port to 1883. Fill in the username and password if there are any or just leave them blank if no access control is used.
After successful execution, the command line will establish the connection, publish the message and then disconnect from the broker, and the command line window in Step 2 will print the following message:
topic: testtopic/1
payload: from MQTTX CLI
1 2
Parameters:
-t: Subscription topic
-q: QoS of the message (default: 0)
-h: IP address of the listener (default: localhost)
EMQX Dashboard provides a WebSocket client as a quick and highly effective MQTT test tool. With this MQTT over WebSocket, you can test connecting to EMQX, subscribing to topics, and publishing messages.
In EMQX Dashboard, click Diagnose -> WebSocket Client on the left navigation tree.
Fill in the connection information in the Connection section. For Host, fill in the corresponding IP address (Default: localhost), for Port, keep the default 8083, and then fill in the user name and password if there are any, or just leave them blank.
Click the Connect button to establish a connection.
Set the topic to subscribe in the Subscription section. Here we will fill in testtopic/# as Topic, select the corresponding QoS, and then click the Subscribe button to finish the subscription. testtopic/# will be added to the table below. After the subscription, all messages matching the topic will be forwarded to this connection and added to the Received section.
Set the topic for the message to be published in the Publish section. Fill in the topic in the Topic field (wildcards like + and # are not supported), here we will fill in testtopic/1, fill in the message body under Payload, select the corresponding QoS, and whether this is a retained message. Click the Publish button, and one record will be added to the Published section below.
The message will be routed to all subscribers, as we have already subscribed to this topic in step 5, one new record will also be added to the Received section below.
Now we have successfully experienced the entire messaging services with the Dashboard WebSocket client.