MQTT Protocol Handshake

MQTT is TCP/IP based protocol . So first there would be TCP connection establishment and then data transfer will  happen . MQTT Communication happens in following three stages.
1)TCP Connection Establishment
2)MQTT Connection Establishment and Data publish
3)TCP Connection Termination

1)TCP Connection Establishment:

TCP connection establishment is a three way handshake. Usually we say first client will send SYN request then server will send acknowledgment ([SYN , ACK]) & then again client will send ACK but Actually three way handshake is achieved by sending 3 packets between client and server . Server (Broker) is always in Listening mode. First Client(Publisher) will inform the server, I want to communicate with you by sending connection  request  Packet with  SYN Flag=1 and ACK =0 . After the reception of  connection  request  Packet , Server will  send the acknowledgment packet with SYN Flag= 1 and ACK= 1  to inform the client that server has  received the connection request packet and ready to communicate with you .Client , After the reception of   SYN Flag=1 & ACK=1 in acknowledgment packet from server , client will send  acknowledgment packet with SYN Flag=0 & ACK=1 to inform the server that client has  received the acknowledgment of connection request and ready to communicate with you. Transmission of data only  happens after the successful TCP handshake.

2)MQTT Connection Establishment and Data publish

After the successful TCP handshake , the first packet send from the publisher to broker is the CONNECT packet which is nothing but the connection request packet to server . After the reception of connection request packet broker will send an acknowledgment packet to publisher indicating that the broker has received the connection request packet  and ready to communicate with you . Once the publisher receives the acknowledgment packet from server or broker , publisher will understand that server is also ready for communication and publisher will start publishing the message . After Publishing the message publisher will disconnect from the broker by sending disconnect request packet . MQTT publisher handshake with broker is different for different QOS level . Above  MQTT publisher handshake with broker is for QOS level 0 .

3)TCP Connection Termination:

TCP connection termination is known as symmetric release. Connection is said to be terminated when client and server send packet with FIN Flag=1 to each other. When client want’s to terminate the communication with server, Client will send disconnect request  packet with FIN Flag=1,  indicating that Client want’s to terminate the communication. After the reception of disconnect request Packet, server will send two packets, first packet would be acknowledgment packet with ACK=1 to client, which informs the client that Server has received the connection termination request and second packet would be connection termination request packet with FIN Flag=1 & ACK=1 from server side, indicating that server also want’s to terminate the connection. After the reception of connection termination packet with FIN Flag=1 & ACK=1 from server , client will understand server has sent termination from his side also, then client will send acknowledgment packet with ACK=1 to server indicating I have received connection termination request from you and terminating connection now.




Publisher & Broker Handshake with Wireshark:

1)TCP Connection Establishment:

Explained in detail above.

2)MQTT Connection Establishment and Data publish

After the successful TCP handshake , the first packet send from the subscriber to broker is the CONNECT packet which is nothing but the connection request packet to server . After the reception of connection request packet broker will send an acknowledgment packet to subscriber indicating that the broker has received the connection request packet  and ready to communicate with you . Once the subscriber receives the acknowledgment packet from the server or broker , subscriber will understand that server is also ready for communication and subscriber will send subscribe request packet with topic name. After the reception of  subscribe request packet broker will send an acknowledgment packet to subscriber.

Subscriber & Broker handshake with Wireshark:




You may also like...

Leave a Reply