Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Establishing a connection to the MQTT broker failed #151

Closed
saradha92 opened this issue Feb 3, 2023 · 7 comments
Closed

Establishing a connection to the MQTT broker failed #151

saradha92 opened this issue Feb 3, 2023 · 7 comments

Comments

@saradha92
Copy link

saradha92 commented Feb 3, 2023

I am using this library for my mosquitto mqtt project. When the connection to broker fails it shows some uncaught error. we are working on a project where we will get an email when the connection to broker failed." Trying to catch the error. But it is getting more complicated. If I catch the error it displays another uncaught type error. I'm attaching the image of the type error too.

PHP Fatal error:  Uncaught PhpMqtt\Client\Exceptions\ConnectingToBrokerFailedException: [1000] Establishing a connection to the MQTT broker failed: Socket error [111]: Connection refused in /home/ubuntu1/mqttbroker/vendor/php-mqtt/client/src/MqttClient.php:250
Stack trace:
#0 /home/ubuntu1/mqttbroker/vendor/php-mqtt/client/src/MqttClient.php(161): PhpMqtt\Client\MqttClient->establishSocketConnection()
#1 /home/ubuntu1/mqttbroker/vendor/php-mqtt/client/src/MqttClient.php(145): PhpMqtt\Client\MqttClient->connectInternal()
#2 /home/ubuntu1/mqttbroker/main.php(35): PhpMqtt\Client\MqttClient->connect()
#3 {main}  thrown in /home/ubuntu1/mqttbroker/vendor/php-mqtt/client/src/MqttClient.php on line 250

type error

@saradha92 saradha92 changed the title Establishing a connection to the MQTT broker failed PHP Fatal error: Uncaught PhpMqtt\Client\Exceptions\ConnectingToBrokerFailedException:Establishing a connection to the MQTT broker failed Feb 3, 2023
@Namoshek Namoshek changed the title PHP Fatal error: Uncaught PhpMqtt\Client\Exceptions\ConnectingToBrokerFailedException:Establishing a connection to the MQTT broker failed Establishing a connection to the MQTT broker failed Feb 3, 2023
@Namoshek
Copy link
Collaborator

Namoshek commented Feb 3, 2023

Have you changed something about the code of the MqttClient? Because this looks rather uncommon and I'm not sure how you'd even reach that point.

@saradha92
Copy link
Author

No I didn't change anything.

@Namoshek
Copy link
Collaborator

Namoshek commented Feb 6, 2023

In that case, this sounds like a firewall issue. Connection refused means that either the broker is not listening on that port or that a firewall in between is blocking the port. You can verify whether a connection can be established by the device which runs the PHP application with either telnet <host> <port> or openssl s_client -connect <host>:<port> </dev/null. If either of these commands hangs or does give you some kind of refusal response, you should look into your firewall.

@saradha92
Copy link
Author

saradha92 commented Feb 6, 2023

Actually I stopped the broker. As I said earlier I'm working on to get an email notification if the connection to broker fails.So What is supposed to be the output is just this oneline "Establishing connection to broker failed". But why I'm getting this uncaught error and stack traces? Due to this my script is not getting executed properly.

@Namoshek
Copy link
Collaborator

Namoshek commented Feb 6, 2023

I'm not sure what your goal is and what your current solution looks like (i.e. your code), but why don't you simply catch the exception and add some error handling?

try {
    $client = new \PhpMqtt\Client\MqttClient($host, $port, $clientId);
    $client->connect();

    // ... your MQTT logic ...
} catch (\PhpMqtt\Client\Exceptions\ConnectingToBrokerFailedException $e) {
    $this->logger->error('The MQTT client could not connect to the broker.');
    
    // Maybe add something like this as well:
    // $this->notificationManager->sendAlert(...);
}

@saradha92
Copy link
Author

I tried catch the error as you mentioned. now I'm getting this error.

PHP Fatal error: Uncaught Error: Cannot access private property PhpMqtt\Client\MqttClient::$logger in /home/ubuntu1/mosquitto/test.php:39

Stack trace:

#0 {main}

thrown in /home/ubuntu1/mosquitto/test.php on line 39

@saradha92
Copy link
Author

Fixed the error. Used printf instead of this->logger->error. And put the catch statement at the end of the publish block to avoid further uncaught exceptions.

Thank you so much for the timely response.

@Namoshek Namoshek closed this as completed Feb 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants