mlesniew / PicoMQTT

ESP MQTT client and broker library
GNU Lesser General Public License v3.0
219 stars 25 forks source link

Client takes a long time to reconnect after restarting the broker #29

Closed ehabtawfikelbwab closed 1 month ago

ehabtawfikelbwab commented 4 months ago

Hello,

I have a broker and client everything works fine But when I restart the broker and it has already started but the clients are taking a long time to reconnect

i need to reconnect to broker faster

i did try this one and did not make a different

  mqtt.reconnect_interval_millis = 1000;
  mqtt.begin();
mlesniew commented 4 months ago

I suspect the problem is not in the handling of reconnects.

Try adding connect and disconnect callbacks that will print messages to serial like this:

    mqtt.connected_callback = [] {
        Serial.println("MQTT connected");
    }

    mqtt.disconnected_callback = [] {
        Serial.println("MQTT disconnected");
    }

With this code added to setup() try again and observe when the messages are printed.

If it turns out that there's a big delay between MQTT disconnected and MQTT connected, then this is a bug within the reconnect logic.

However, I suspect that something else is happening. When the broker is restarted, it may not close the TCP properly. There's no way for the client to know that it's peer is no longer there, so it will wait for new packets on its TCP socket. Eventually, it will give up after a timeout and treat it as a disconnect.

If that's what is happening, you will see MQTT disconnected long after the broker is restarted. MQTT connected will be displayed after that within around a second or less.

ehabtawfikelbwab commented 3 months ago

Hi there, Yes i got big delay between MQTT disconnected and MQTT connected

How can i fix this problem?

mhaberler commented 3 months ago

what is your client?

without that very hard to help you. Why not post your code in a repo?

I have seen clients which do the right thing on a TCP reset, and some dont.

ehabtawfikelbwab commented 3 months ago

Hi Sir,

Here is my code:

PicoMQTT::Client mqtt("192.168.1.100", 1412);

void setup() {
Serial.begin(115200);

connectToWiFi();

// Subscribe to a topic and attach a callback
mqtt.subscribe("d/" + id_, [](const char *payload) {
mqttEvent(payload);
});

mqtt.begin();

mqtt.connected_callback = [] {
    Serial.println("MQTT connected");
}

mqtt.disconnected_callback = [] {
    Serial.println("MQTT disconnected");
}
Serial.println("Setup completed");
}

void loop() {
mqtt.loop(); //Handle mqtt connection
}

it is connect normally without no problem and working fine But when I restart the broker and it has already started but the clients are taking a long time to reconnect i need to reconnect to broker faster

mhaberler commented 3 months ago

can you explain "long time to reconnect" in seconds?

ehabtawfikelbwab commented 3 months ago

it is not static I did try to calculate the time between "MQTT connected" and "MQTT disconnected" and sometime 2 sec sometime 10 , 20 , 30, 40 and even more

mlesniew commented 3 months ago

The code is currently missing the reconnect_interval_millis assignment. Try setting it to 1000 and see if it helps.

ehabtawfikelbwab commented 3 months ago

I did try reconnect_interval_millis with 1000 value ,500 and 150 and it is the same problem i did try to debug everything i could and everything is fine

I don't know why this happening

client : nodemcu esp12 broker: esp32

mlesniew commented 2 months ago

Hey @ehabtawfikelbwab! Can you retry the experiment that I suggested on March 26th with the latest version of PicoMQTT?

There was a bug in how the callbacks were fired, it's fixed in version 1.0.0. Can you see if you still get the same result?

Again, what we want to know is if there's a big time delay between when it says it got disconnected and connectected after the broker restart.

mlesniew commented 1 month ago

@ehabtawfikelbwab any updates on this?

ehabtawfikelbwab commented 1 month ago

@mlesniew Yes the problem fixed by modifying keep_alive_millis in client setting

Old code (the problem): PicoMQTT::Client mqtt("192.168.1.100", 1412);

The default client setting of the library:

Client(const char * host = nullptr, uint16_t port = 1883, const char * id = nullptr, const char * user = nullptr,
               const char * password = nullptr, unsigned long reconnect_interval_millis = 5 * 1000,
               unsigned long keep_alive_millis = 60 * 1000, unsigned long socket_timeout_millis = 10 * 1000)

The fix without auth : PicoMQTT::Client mqtt("192.168.1.100", 1412, **(Your_Client_uid**), nullptr, nullptr, 200, 3000, 5000);

The fix with auth: PicoMQTT::Client mqtt("192.168.1.100", 1412, **(Your_Client_uid)**, **(Your_Username)**, **(Your_Password)**, 200, 3000, 5000);

The fix:

You can use your own setting but this setting is good for my project. Thank you very much!

mlesniew commented 1 month ago

Great to hear it works for you now!