Rain
Rain

Reputation: 105

MQTT/TCP Timeout

I wrote a simple program used to observe the MQTT connection state.

MQTT library choice <PubSubClient.h>

Only do MQTT Connect, but not do other things, like client.loop(keep connect)、disconnect.

When connecting successfully, and wait about 22s after, TCP sends [FIN, ACK] and [RST, ACK].

Is TCP timeout ? And how to know TCP timeout value and view sessions from Wireshark .

Client: ESP8266 <-> MQTT Broker: Windows

Code:

    void MQTT_Setup(){
      client.setServer(MQTT_SERVER, MQTT_PORT);
      //client.setCallback(callback);
      MQTT_Connect();
    }
    
    void MQTT_Connect(){
      bool bMQTT;
      bMQTT = client.connect("ESP");
      Serial.println("MQTT Connected");
      if(bMQTT)MQTT_Disconnect();
      //MQTT_Publish();
    }
    void setup() {
    Serial.begin(BAUDRATE);
    delay(3000);
    WIFI_Setup();
    MQTT_Setup();
    }

    void loop() {
      //client.loop();
    }

Wireshark: enter image description here

Upvotes: 2

Views: 3664

Answers (1)

hardillb
hardillb

Reputation: 59608

The default MQTT Keep alive value for the PubSubClient can be found on line 36 of the PubSubClient.h

The default is 15 seconds, brokers will wait for 1.5 the keep alive and if they have not received a packet from the client drop the connection.

15 * 1.5 ~= 22 seconds

This is why the connection is being closed. The client.loop() function will send the required ping packets to keep the connection alive if needed.

Upvotes: 1

Related Questions