Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Mosquitto » Cannot (reconnect) to mosquitto after broker restarted
Cannot (reconnect) to mosquitto after broker restarted [message #1849822] Thu, 03 February 2022 15:15 Go to next message
Tamir Michael is currently offline Tamir MichaelFriend
Messages: 2
Registered: February 2022
Junior Member
Hello and thanks for reading,

I have trouble re-connecting to mosquitto after it has been restrated.
At first, my program is registered to mosquitto as expected. However, after the broker is restarted (creating a new instance of the broker; I have no control over that) I must re-establish connection with it but the call to connect() below hangs:

mosquitto_lib_cleanup();
reinitialise(("mosq/" + m_ClientId + "/" + m_ServiceName + "//").c_str(), true);
if (mosqpp::lib_init() != MOSQ_ERR_SUCCESS)
{
    throw std::runtime_error("CMQTTCommunicator: can't intialize library.");
}

mosqpp::mosquittopp::int_option(MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5);
// Last will and testament
const std::string WillMessage = Json(Json::object{{ "service", m_ServiceName }, {"status", "Disconnected"}}).dump();
will_set((m_FullServiceIdentifier + "status").c_str(), WillMessage.length(), WillMessage.c_str(), static_cast<int>(EQualityOfService::DeliverAtLeastOnce));

if (connect(m_MQTTBrokerAddress.GetIPAddress().ToString().c_str(), m_MQTTBrokerAddress.GetPort(), KeepAliveSec) != MOSQ_ERR_SUCCESS)
{
    throw CApplicationException("CMQTTCommunicator: could not connect to mosquitto broker");
}


I do not think I can use reconnect() here at the broker is restarted.
Do you see why this is the case? What am I missing?

Thanks in advance!

[Updated on: Fri, 04 February 2022 08:04]

Report message to a moderator

Re: Cannot (reconnect) to mosquitto after broker restarted [message #1849827 is a reply to message #1849822] Thu, 03 February 2022 16:35 Go to previous messageGo to next message
Roger Light is currently offline Roger LightFriend
Messages: 90
Registered: September 2013
Member
Hi,

I would suggest using loop_start() or loop_forever() instead of manually using loop(), assuming you are doing. Both loop_start() and loop_forever() take care of reconnecting for you. That may be why you're having problems if the library is trying to reconnect and you are doing the same.

Regards,

Roger
Re: Cannot (reconnect) to mosquitto after broker restarted [message #1849844 is a reply to message #1849827] Fri, 04 February 2022 09:29 Go to previous message
Tamir Michael is currently offline Tamir MichaelFriend
Messages: 2
Registered: February 2022
Junior Member
Hello,
Thanks for your reply.
Unfortunately this does not change the situation: occasionally reconnection fails (it is not easy for me to debug this, but from the behavior I observe it is clear connection is not established).
To be clear: the method you suggested should always work, even if a new instance of the broker is spawned?

[Updated on: Fri, 04 February 2022 09:30]

Report message to a moderator

Previous Topic:Mosquitto Broker Bridge-mode: Why i'm recieving the published message one more time than expected?
Next Topic:MQTT over WebSockets with TLS
Goto Forum:
  


Current Time: Fri Apr 26 05:34:32 GMT 2024

Powered by FUDForum. Page generated in 0.03286 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top