Skip to content

Conversation

Namoshek
Copy link
Collaborator

@Namoshek Namoshek commented Jan 8, 2022

This PR addresses the problem described in #18 where manually closing a connection will not remove the connection from the ConnectionManager:

// First job
$mqtt = MQTT::connection(); // connected
$mqtt->publish($topic, $data, $qos);
$mqtt->disconnect();

// nth job
$mqtt = MQTT::connection(); // disconnected

As a solution to this problem, we simply reinitialize the connection on retrieval. Another option would be to reconnect using the old instance, but this approach will always yield a fresh instance.

Known issues:
$mqtt->isConnected() will not detect connection loss based on closed sockets, socket timeouts and similar. To do so, we would need to ping the MQTT broker, which is currently not easily possible due to the way data is sent and received. Imagine the following scenario:

// First job
$mqtt = MQTT::connection(); // connected
$mqtt->subscribe($topic, $callback);

// nth job
$mqtt = MQTT::connection(); // connected

// Cannot send PINGREQ and wait for PINGRESP because the socket may have published messages
// from the subscription waiting
$mqtt->isConnected();

@Namoshek Namoshek added the bug Something isn't working label Jan 8, 2022
@Namoshek
Copy link
Collaborator Author

Namoshek commented Jan 8, 2022

@stijn1989 Does this look like a suitable solution to you?

@stijn1989
Copy link

Yes, this should solve the isseu. 👍

@Namoshek Namoshek merged commit 7dfaac0 into master Jan 8, 2022
@Namoshek Namoshek deleted the fix-reconnect-if-disconnected branch January 8, 2022 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants