MQTT Packet Structure
MQTT control packet structure is nothing but the MQTT packet structure. MQTT packet has three fields and those are Fixed header, Variable header and Payload. The fixed header field is present in all the packet and the size is 2 bytes. Variable header and Payload are not present in all packets. The size of Variable header and Payload is variable. As the size of the variable header and payload is variable, the overall size of the MQTT packet size is also variable. The MQTT Packet structure is shown below.
Please click on the below link to become master in MQTT.
MQTT Control packet Structure:
Fixed header is always present in each MQTT packet . It is two bytes in length. Detail structure of Fixed header is given below.
Message Type: Message type represents a connection request type. Each value of message type represents different connection request. It is 4 bit in length. Following table shows the value of message request and it’s meaning as well as the presence of fixed header in all message request.
Header Flags :
There are 3 Header flags and those are DUP , QOS and RETAIN.
DUP is one bit(bit 3) , QOS is 2 bit(bit 1 and bit 2) and RETAIN is one bit(bit 0).
1)DUP Flag: Duplicate delivery of publish control packet. If DUP=0 means this is Clients first attempt to send MQTT publish control packet. If DUP=1 means client is again trying to send previously sent packet.
2)QOS level:Quality of service (QOS) indicates the assurance of message delivery. There are three 3 QOS . It is 2 bit in length.
3)RETAIN: If RETAIN=1 means Broker or server will hold or store the packet unless there no subscriber with same topic as topic in stored packet . As soon as there is subscriber , Broker will deliver the stored packet. If RETAIN=0 means broker will not hold the packet.
Remaining length: Number of bytes left within current packet, including variable header plus payload.
It’s not present in all MQTT control packets. It’s structure is different for different MQTT request that will see later with wireshark packets. Following table shows, in which request Variable header is present or not.
Payload is actual data which is to be send. It’s not present in all the MQTT control packets. Following table shows, in which request Payload is required or not.
MQTT Packet Structure OR Format Analysis with Wireshark :
1)CONNECT : MQTT is TCP/IP based protocol so first there would be TCP connection establishment. CONNECT request Packet is the first packet send by Client to Broker or Server after TCP connection establishment . All the three fields are present in CONNECT request packet.
Variable Header Fields:
1)Protocol Name Length: This Field shows Protocol name Length. protocol name Length is 4.
2)Protocol Name: This Field shows Protocol Name. protocol Name is MQTT.
3)Version: This field shows Protocol version. It is version 4.
4)Connect Flag Bits: Its 8 bit in Length. Each bit is explained in detail below .
User Name Flag(bit 7):If User name Flag is 1 means user name must be present in payload.If User name Flag is 0 means user name must not be present in payload.
Password Flag(bit 6):If Password Flag is 1 means Password must be present in payload.If Password Flag is 0 means Password must not be present in payload.If user name flag is 0 then Password flag must be zero.
Will Retain(bit 5):If Will Flag is 0 then Will Retain flag should be zero.If Will Retain is 0 means message won’t be retain by server or broker.If Will Retain is 1 means message will be retain by server or broker.
Will QoS(bit 3 and 4):If Will Flag is 0 then Will QoS flag should be zero.If will Flag is 1 then Will QoS can be 0 or 1 or 2. Please See above explained QoS level table for QoS understanding .
Will Flag(bit 2):If Will Flag is 1 means Will QoS and Will Retain fields in connect flags will be used by server and will topic and will message must be present in payload.If Will Flag is 0 means Will QoS and Will Retain fields in connect flags must be set to 0 and will topic and will message must not be present in payload.
Clean Session(bit 1):If this bit is 1 means Client and Server must discard previous session states and start a new session.If this bit is 0 means carry last session states,If last session is not existing then start new communication with a new session.
Reserved(bit 0):Reserved for future implementation.
5)Keep Alive:It is Duration. when subscriber is IDLE means not not receiving data from broker, then connection is maintained by sending PING request from subscriber. Duration after which subscriber sending PING request to broker again and again to maintain a connection is nothing but the keep alive duration. Default keep alive Duration is 60 sec.
Payload: Payload of CONNECT packet has Client Identifier , Client Identifier Length , Will Topic , Will Message , User Name , Password.
Client Identifier(Client ID): Client Identifier is nothing but the name of client connecting to the server or broker. Each client should have unique Client Identifier. This Client Identifier helps server to identify the states of clients.
Client Identifier(Client ID) Length : This field shows Client identifier length.
2)CONNACK : CONNACK packet is sent from Broker to Client when broker receives CONNECT request packet . Only Fixed header and Variable header fields are present in CONNACK packet. Payload is not present in CONNACK packet.
Variable header Fields:
Acknowledgement Flags: It is 8 bit in length. Each field explained in detail below.
Reserved(bits 7-1): Bits 7-1 are reserved for future implementation.
Session Present(bit 0): If server accepts connection with Clean Session flag=1 means server has accepted new connection request and in return of that connection request server will set Session Present bit to 0 means server is available to accept connection and client can connect to server. If server accepts connection with Clean Session flag=0 ,the value of Session present flag depends on whether the server already has stored session states or not for that particular Client ID.If states are already stored for that particular Client ID the Session present is set to zero if not then Session Present flag set to 1.
Return Code : Return code is send by broker to client. Return code and its meaning explained in following table.
3)PUBLISH : PUBLISH packet is send by Publisher to broker when publisher wants to publish or send data to broker.
Variable header Fields:
Topic Length: This field shows length of topic name.
Topic:This Field shows topic name on which data is being publish.
Payload Field :
Message:Actual data which is being publish or send.
4)PUBACK : PUBACK is an acknowledgement send by Broker to Publisher only when publisher will publish message with QOS level 1.
5)PUBREC : PUBREC is an acknowledgement send by Broker to Publisher only when publisher will publish message with QOS level 2 .
6)PUBREL: PUBREL is send by Publisher to broker when publisher receives PUBREC from broker .
7)PUBCOMP : PUBCOMP is send by Broker to Publisher only when broker receives PUBREL from publisher.
8)SUBSCRIBE : SUBSCRIBE packet is send by Subscriber to broker when subscriber wants to receive data from publisher.
1)Topic Length: Length of topic which is being subscribe .
2)Topic: Name of topic which is to be subscribe .
3)Requested QOS: QOS of subscription .
9)SUBACK : SUBACK packet is an acknowledgment packet send by broker to subscriber when subscriber receives SUBSCRIBE request packet.
Payload of SUBACK acknowledgment packet has Granted QoS level from broker.
Granted QoS value=0 means QoS level 0 Granted
Granted QoS value=1 means QoS level 1 Granted
Granted QoS value=2 means QoS level 2 Granted
10)PINGREQ : PINGREQ request packet is send by subscriber to broker for maintaining a connection when subscriber is IDLE. PINGREQ Request packet doesn’t have Variable Header as well as Payload.
11)PINGRESP : PINGRESP packet is send by broker to subscriber in the reply of PINGREQ request packet. PINGRESP packet doesn’t have Variable Header as well as Payload.
12)DISCONNECT :It is a last packet send from client to broker when client wants to disconnect. DISCONNECT Request packet doesn’t have Variable Header as well as Payload.