Mosquitto-PHP icon indicating copy to clipboard operation
Mosquitto-PHP copied to clipboard

Throwing an exception instead of reconnecting

Open bitfreak25 opened this issue 8 years ago • 3 comments

As I tested reconnecting by using loopForever() and restarting the local mqtt-broker mosquitto, my script stops. The output was:

PHP Fatal error: Uncaught exception 'Mosquitto\Exception' with message 'The connection was lost.' in /home/user/public_html/test.php:41 Stack trace:

0 /home/user/public_html/test.php(41): Mosquitto\Client->loopForever()

1 {main}

thrown in /home/user/public_html/test.php on line 41

I expected a reconnect, but this wasn't the case. Whats wrong here?

bitfreak25 avatar Aug 01 '16 09:08 bitfreak25

@bitfreak25 Hello sir, How did you solved this issue ?

abelkbil avatar Oct 18 '16 14:10 abelkbil

I used to use the mosquitto_loop_forever() function from the underlying library to handle the reconnects, but unfortunately that means that the function never returns at all, so if you attempt to break out of the loop, throw an exception, or similar, PHP doesn't know about it. That means I need to reimplement the function myself, which I've done, but I didn't get to implementing all of the reconnection logic. That's on my to-do list, and I hope to have it ready to look at in the next few days. In the mean time I'm afraid you need to do it the long way in your own code.

mgdm avatar Oct 18 '16 20:10 mgdm

I solved it by using a php-restart-script similar to an answer on stackoverflow. But for me this looks like a workaround, so it would be nice, if this will be implemented in the library itself.

bitfreak25 avatar Oct 19 '16 07:10 bitfreak25