Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [mosquitto-dev] some questions about libmosquitto

On Fri, 18 Mar 2022 09:32:27 +0100
Francesco Stefanni <francesco.stefanni@xxxxxxxxxxxxxxxx> wrote:

> I am trying to use libmosquitto client API into a project, using the
> async api,
> but the followings are not clear to me :
> 
>    1. I like to perform a check to see the connection status, but no
>    "mosquitto_is_connected()" or similar method is provided. Is there
> a way to know the status?
I don't know of a way to poll for connection status. Remember MQTT was
designed for very low bandwidth links in SCADA and similar systems.
To minimise traffic, MQTT does not have to send any packets when there
are no messages to be sent (published / received). There is a PINGREQ /
PINGRESP system to allow keep-alive traffic, but the interval between
messages can be long (up to 18 hours) and it can be disabled.
https://www.hivemq.com/blog/mqtt-essentials-part-10-alive-client-take-over/

So the honest answer is that libmosquitto doesn't really know if it is
connected, unless it gets a definite connection closure, in which case
the disconnect callback will be called.

>    2. I do not understand when the callback set with
>    "mosquitto_disconnect_callback_set()" is called. Is it on
> unexpected disconnections? Or is it called when I call
> "mosquitto_disconnect()"? Or both?
Both. See the callback parameter documentation for the `rc` parameter
which tells the callback which has happened.
https://mosquitto.org/api/files/mosquitto-h.html#mosquitto_disconnect_callback_set

>    3. If I understand correctly, the async api should reconnect
>    automatically in case of disconnection. Will it also automatically
>    re-subscribe to the topics?
As you will have seen in the thread "Reconnection to a TLS server is
unreliable" on this list, the answer to your third question is no -
which is why you should subscribe (or re-subscribe) to topics in the
connected callback.

Richard


Back to the top