MQTT Publish


Once the MQTT client connects to a MQTT broker, they can publish their messages using MQTT publish method.

MQTT Publish messages should contain,

  • packetId – Unique message identifier is managed by the MQTT client and the broker.
  • TopicName – Name of the topic that the message will be published under.
  • QoS – There are 3 levels of Quality of Service, QoS levels define the kind of guarantee that a message will reach the intended recipient.
  • retainFlag – This flag indicated whether the message sent by the client should be saved as the last good value for the topic.
  • Payload – This is the actual message content and can contain images, text in any encoding format.
  • DUP Flag – This flag indicates whether the message is a duplicate.

Related Topics

Try AT Command Tester tool to test MQTT features with a local install of the MQTT broker.

MQTT Publish Server Logs

Below log shows MQTT Publish messages between the MQTT client and the MQTT Broker.

C:\Program Files\mosquitto>mosquitto -v
1556978695: mosquitto version 1.6.0 starting
1556978695: Using default config.
1556978695: Opening ipv6 listen socket on port 1883.
1556978695: Opening ipv4 listen socket on port 1883.
1556978751: New connection from 127.0.0.1 on port 1883.
1556978751: New client connected from 127.0.0.1 as at_tester_2944 (p2, c1, k60).
1556978751: No will message specified.
1556978751: Sending CONNACK to at_tester_2944 (0, 0)
1556978759: Received PUBLISH from at_tester_2944 (d0, q1, r0, m1, ‘SensorReadings’, … (19 bytes))
1556978759: Sending PUBACK to at_tester_2944 (m1, rc0)
1556978761: Received PUBLISH from at_tester_2944 (d0, q1, r0, m2, ‘SensorReadings’, … (19 bytes))
1556978761: Sending PUBACK to at_tester_2944 (m2, rc0)
1556978763: Received PUBLISH from at_tester_2944 (d0, q1, r0, m3, ‘SensorReadings’, … (19 bytes))
1556978763: Sending PUBACK to at_tester_2944 (m3, rc0)
1556978765: Received PUBLISH from at_tester_2944 (d0, q1, r0, m4, ‘SensorReadings’, … (19 bytes))
1556978765: Sending PUBACK to at_tester_2944 (m4, rc0)
1556978767: Received PUBLISH from at_tester_2944 (d0, q1, r0, m5, ‘SensorReadings’, … (19 bytes))
1556978767: Sending PUBACK to at_tester_2944 (m5, rc0)
1556978769: Received PUBLISH from at_tester_2944 (d0, q1, r0, m6, ‘SensorReadings’, … (19 bytes))
1556978769: Sending PUBACK to at_tester_2944 (m6, rc0)
1556978771: Received PUBLISH from at_tester_2944 (d0, q1, r0, m7, ‘SensorReadings’, … (19 bytes))
1556978771: Sending PUBACK to at_tester_2944 (m7, rc0)
1556978773: Received PUBLISH from at_tester_2944 (d0, q1, r0, m8, ‘SensorReadings’, … (19 bytes))
1556978773: Sending PUBACK to at_tester_2944 (m8, rc0)
1556978775: Received PUBLISH from at_tester_2944 (d0, q1, r0, m9, ‘SensorReadings’, … (19 bytes))
1556978775: Sending PUBACK to at_tester_2944 (m9, rc0)
1556978777: Received PUBLISH from at_tester_2944 (d0, q1, r0, m10, ‘SensorReadings’, … (19 bytes))
1556978777: Sending PUBACK to at_tester_2944 (m10, rc0)
1556978779: Received PUBLISH from at_tester_2944 (d0, q1, r0, m11, ‘SensorReadings’, … (19 bytes))
1556978779: Sending PUBACK to at_tester_2944 (m11, rc0)
1556978781: Received PUBLISH from at_tester_2944 (d0, q1, r0, m12, ‘SensorReadings’, … (19 bytes))
1556978781: Sending PUBACK to at_tester_2944 (m12, rc0)
1556978783: Received PUBLISH from at_tester_2944 (d0, q1, r0, m13, ‘SensorReadings’, … (19 bytes))
1556978783: Sending PUBACK to at_tester_2944 (m13, rc0)
1556978785: Received PUBLISH from at_tester_2944 (d0, q1, r0, m14, ‘SensorReadings’, … (19 bytes))
1556978785: Sending PUBACK to at_tester_2944 (m14, rc0)

Let us look at the first publish message from the client that was processed by  the MQTT broker.

1556978759: Received PUBLISH from at_tester_2944 (d0, q1, r0, m1, ‘SensorReadings’, … (19 bytes))

MQTT client sent publish message with below settings,

  • d0 – Dupe flag is 0, this is not a dupe message
  • q1 – Quality of Service level is 1
  • r0 – Retain flag is 0
  • m1 – This is the unique message identifier
  • ‘SensorReading’ – This is the topic name
  • Payload – The server log doesn’t show the payload itself, but it is 19 bytes. As shown in the above image, the first payload is ‘sensor_data=209.997’ which is 19 bytes long.

1556978759: Sending PUBACK to at_tester_2944 (m1, rc0)

The MQTT broker acknowledged the message by sending the PUBACK response with the packet identifier.

Keywords: Module , M2M , GPRS , 3G , through AT commands,