philbowles / PangolinMQTT

PangolinMQTT - ArduinoIDE client library for ESP8266, ESP32 and STM32-NUCLEO
Other
71 stars 21 forks source link

Fails to connect if messages sent before connected #21

Closed einglis closed 4 years ago

einglis commented 4 years ago

My state machine attempts to connect to MQTT, then polls checking for connection (I could use an onConnect() callback, but I don't). Once connected, I publish and subscribe. So far so good.

However, due to https://github.com/philbowles/PangolinMQTT/issues/19, as soon as I start the connect, I think I'm connected and start publishing etc immediately. This confuses Pangolin, and it never successfully connects. In fact, the MQTT server reports a socket error and disconnects, and when Pangolin times out after ten seconds, the sequence repeats.

It's clearly an error on my part to publish etc before connection (and if I apply the fix suggested in #19, all is good), but it shouldn't be 'fatal' to Pangolin's chances of successful connection.

philbowles commented 4 years ago

Patient: "Doctor, every time I do this, it hurts!" Doctor: "Then don't do it"

My state machine attempts to connect to MQTT, then polls checking for connection (I could use an onConnect() callback, but I don't).

Then do. Pangolin is an async library. Running it from polling code is not only not very sensible or efficient, but is using it in a way for which it was not designed and thus all bets are off. You need to restructure your code to respond to the connect / disconnect hooks.

as soon as I start the connect, I think I'm connected and start publishing etc immediately

Again, you are using Pangolin wrongly. You cannot publsih until the onConnect callback has occurred. The documentation and all the examples makes this clear and obvious. I cannot be responsible for bad user code causing unpredictable problems.

I think I'm connected

Then you are wrong. And why would you - there is nothing in the documentation that suiggests this is the case - as above you are not connected until the onConnect callback is called - the clue is in the name 👍

It's clearly an error on my part to publish etc before connection

Correct. You have completely breached the MQTT protocol - The MQTT specification requires that the network connection is closed when a protocol breach occurs and this is why you see the socket error.

it shouldn't be 'fatal' to Pangolin's chances of successful connection.

Why shouldn't it? Pangolin correctly closes the connection on the breach of protocol. If your code then repeats itself, breaching the protocol again then - guess what - Pangolin will correctly close the connection again.

You need to make your code confom to the way that Pangolin is designed to work.

einglis commented 4 years ago

You have a very provocative style. It must be nice to be right all the time.