MQTT Subscriber
MQTT clients can subscribe to topics once they are connected successfully to a MQTT broker.
MQTT Subscrbe message should contain
- packetId – Unique message identifier is managed by the MQTT client and the broker.
- List of subscription – Clients can subscribe to multiple topics within the same Subscribe message. The Quality of Service is included for each topic.
Format of MQTT Subscription message
packetId 7676
qos1 1
topic1 “mytopic/1”
qos2 0
topic2 “mytopic/2”
Related Topics
- How MQTT works?
- MQTT – Connect to Broker
- MQTT Topics
- MQTT Publish
- MQTT Subscribe
- MQTT Quality of Service (QoS
- MQTT – Keep Alive with PINGREQ and PINGRESP
- Install MQTT broker Mosquitto in Windows
- Mosquitto Broker – Enable user authentication
- MQTT Broker connection with user name and password
Try AT Command Tester tool to test MQTT subscribe feature with a local install of the MQTT broker.
In the below setup, 2 MQTT clients connect to the Mosquitto MQTT broker that is installed on the local machine. MQTT client 1 (of the left) publishes to a topic. MQTT client 2 (on the bottom right) is subscribed to the topics and receives it from the MQTT broker (top right).
MQTT Publish Server Logs
C:\Program Files\mosquitto>mosquitto -v
1557097586: mosquitto version 1.6.0 starting
1557097586: Using default config.
1557097586: Opening ipv6 listen socket on port 1883.
1557097586: Opening ipv4 listen socket on port 1883.
1557097657: New connection from 127.0.0.1 on port 1883.
1557097658: New client connected from 127.0.0.1 as 5236 (p2, c1, k60).
1557097658: No will message specified.
1557097658: Sending CONNACK to 5236 (0, 0)
1557097661: New connection from 127.0.0.1 on port 1883.
1557097661: New client connected from 127.0.0.1 as at_tester_2932 (p2, c1, k60).
1557097661: No will message specified.
1557097661: Sending CONNACK to at_tester_2932 (0, 0)
1557097677: Received PUBLISH from 5236 (d0, q1, r0, m1, ‘SensorReadings’, … (19 bytes))
1557097677: Sending PUBACK to 5236 (m1, rc0)
1557097679: Received PUBLISH from 5236 (d0, q1, r0, m2, ‘SensorReadings’, … (19 bytes))
1557097679: Sending PUBACK to 5236 (m2, rc0)
1557097679: Received SUBSCRIBE from at_tester_2932
1557097679: SensorReadings (QoS 1)
1557097679: at_tester_2932 1 SensorReadings
1557097679: Sending SUBACK to at_tester_2932
1557097681: Received PUBLISH from 5236 (d0, q1, r0, m3, ‘SensorReadings’, … (19 bytes))
1557097681: Sending PUBACK to 5236 (m3, rc0)
1557097681: Sending PUBLISH to at_tester_2932 (d0, q1, r0, m1, ‘SensorReadings’, … (19 bytes))
1557097681: Received PUBACK from at_tester_2932 (Mid: 1, RC:0)
1557097683: Received PUBLISH from 5236 (d0, q1, r0, m4, ‘SensorReadings’, … (19 bytes))
1557097683: Sending PUBACK to 5236 (m4, rc0)
1557097683: Sending PUBLISH to at_tester_2932 (d0, q1, r0, m2, ‘SensorReadings’, … (19 bytes))
1557097683: Received PUBACK from at_tester_2932 (Mid: 2, RC:0)
1557097685: Received PUBLISH from 5236 (d0, q1, r0, m5, ‘SensorReadings’, … (19 bytes))
1557097685: Sending PUBACK to 5236 (m5, rc0)
1557097685: Sending PUBLISH to at_tester_2932 (d0, q1, r0, m3, ‘SensorReadings’, … (19 bytes))
1557097685: Received PUBACK from at_tester_2932 (Mid: 3, RC:0)
1557097687: Received PUBLISH from 5236 (d0, q1, r0, m6, ‘SensorReadings’, … (19 bytes))
1557097687: Sending PUBACK to 5236 (m6, rc0)
1557097687: Sending PUBLISH to at_tester_2932 (d0, q1, r0, m4, ‘SensorReadings’, … (19 bytes))
1557097687: Received PUBACK from at_tester_2932 (Mid: 4, RC:0)
1557097689: Received PUBLISH from 5236 (d0, q1, r0, m7, ‘SensorReadings’, … (19 bytes))
1557097689: Sending PUBACK to 5236 (m7, rc0)
1557097689: Sending PUBLISH to at_tester_2932 (d0, q1, r0, m5, ‘SensorReadings’, … (19 bytes))
1557097689: Received PUBACK from at_tester_2932 (Mid: 5, RC:0)
1557097691: Received PUBLISH from 5236 (d0, q1, r0, m8, ‘SensorReadings’, … (19 bytes))
1557097691: Sending PUBACK to 5236 (m8, rc0)
1557097691: Sending PUBLISH to at_tester_2932 (d0, q1, r0, m6, ‘SensorReadings’, … (19 bytes))
1557097691: Received PUBACK from at_tester_2932 (Mid: 6, RC:0)
1557097693: Received PUBLISH from 5236 (d0, q1, r0, m9, ‘SensorReadings’, … (19 bytes))
1557097693: Sending PUBACK to 5236 (m9, rc0)
1557097693: Sending PUBLISH to at_tester_2932 (d0, q1, r0, m7, ‘SensorReadings’, … (19 bytes))
1557097693: Received PUBACK from at_tester_2932 (Mid: 7, RC:0)
1557097695: Received PUBLISH from 5236 (d0, q1, r0, m10, ‘SensorReadings’, … (19 bytes))
1557097695: Sending PUBACK to 5236 (m10, rc0)
1557097695: Sending PUBLISH to at_tester_2932 (d0, q1, r0, m8, ‘SensorReadings’, … (19 bytes))
1557097695: Received PUBACK from at_tester_2932 (Mid: 8, RC:0)
1557097697: Received PUBLISH from 5236 (d0, q1, r0, m11, ‘SensorReadings’, … (19 bytes))
1557097697: Sending PUBACK to 5236 (m11, rc0)
1557097697: Sending PUBLISH to at_tester_2932 (d0, q1, r0, m9, ‘SensorReadings’, … (19 bytes))
1557097697: Received PUBACK from at_tester_2932 (Mid: 9, RC:0)
1557097699: Received PUBLISH from 5236 (d0, q1, r0, m12, ‘SensorReadings’, … (19 bytes))
1557097699: Sending PUBACK to 5236 (m12, rc0)
1557097699: Sending PUBLISH to at_tester_2932 (d0, q1, r0, m10, ‘SensorReadings’, … (19 bytes))
1557097699: Received PUBACK from at_tester_2932 (Mid: 10, RC:0)
1557097701: Received PUBLISH from 5236 (d0, q1, r0, m13, ‘SensorReadings’, … (19 bytes))
1557097701: Sending PUBACK to 5236 (m13, rc0)
1557097701: Sending PUBLISH to at_tester_2932 (d0, q1, r0, m11, ‘SensorReadings’, … (19 bytes))
1557097701: Received PUBACK from at_tester_2932 (Mid: 11, RC:0)
1557097703: Received PUBLISH from 5236 (d0, q1, r0, m14, ‘SensorReadings’, … (19 bytes))
1557097703: Sending PUBACK to 5236 (m14, rc0)
1557097703: Sending PUBLISH to at_tester_2932 (d0, q1, r0, m12, ‘SensorReadings’, … (19 bytes))
1557097703: Received PUBACK from at_tester_2932 (Mid: 12, RC:0)
Let us analyse the log,
- MQTT Client 1 (5236) and MQTT Client 2 (at_tester_2932) connects to the MQTT broker on the local machine.
- Client 1 publishes messages for topic ‘SensorReadings’
- Client 2 subscribes to topic ‘SensorReadings’ and receives the messages from teh MQTT broker.