arthur
arthur

Reputation: 1

php-mqtt/client subscribing fails

I'am trying to get messages over mqtt to my webserver written in php. For mqtt I used https://github.com/php-mqtt/client this for php.

My Problem now is this line: $mqtt->load(true);. When I try to open my Website (the website is on my raspberry-pi apache2) the website does not open! Instead the website loads very long till this error appears.

Fatal error: Uncaught PhpMqtt\Client\Exceptions\DataTransferException: [65] Transferring data over socket failed: Sending data over the socket failed. Has it been closed? in /var/www/html/vendor/php-mqtt/client/src/MqttClient.php:1158 Stack trace: #0 /var/www/html/vendor/php-mqtt/client/src/MqttClient.php(1112): PhpMqtt\Client\MqttClient->writeToSocket() #1 /var/www/html/vendor/php-mqtt/client/src/MqttClient.php(1081): PhpMqtt\Client\MqttClient->writeToSocketWithAutoReconnect() #2 /var/www/html/vendor/php-mqtt/client/src/MqttClient.php(696): PhpMqtt\Client\MqttClient->ping() #3 /var/www/html/vendor/php-mqtt/client/src/MqttClient.php(652): PhpMqtt\Client\MqttClient->loopOnce() #4 /var/www/html/phptest2.php(13): PhpMqtt\Client\MqttClient->loop() #5 {main} thrown in /var/www/html/vendor/php-mqtt/client/src/MqttClient.php on line 1158

My Code: (In github is the same code, I only use it to test)

<?php
require "vendor/autoload.php";
$server   = 'broker.hivemq.com';
$port     = 1883;
$clientId = 'test-subscriber';

use PhpMqtt\Client\MQTTClient;
$mqtt = new \PhpMqtt\Client\MqttClient($server, $port, $clientId);
$mqtt->connect();
$mqtt->subscribe('php-mqtt/client/test', function ($topic, $message, $retained, $matchedWildcards) {
    echo sprintf("Received message on topic [%s]: %s\n", $topic, $message);
}, 0);
$mqtt->loop();
$mqtt->disconnect();
?>

The Programm should display the messages. But it does not work and I have no Ideas why.The Publish method works fine.

Upvotes: 0

Views: 503

Answers (0)

Related Questions