salmanzafar949 / MQTT-Laravel

A simple Laravel Library to connect/publish/subscribe to MQTT broker
https://packagist.org/packages/salmanzafar/laravel-mqtt
103 stars 34 forks source link

Undefined index: qos. #46

Open antmorm opened 3 years ago

antmorm commented 3 years ago

When I try to subscribe to a topic, I get this error:

Undefined index: qos.

Here's how I make a subscription:

Mqtt::ConnectAndSubscribe('test', function ($topic, $msg) { echo "Msg Received: \n"; echo "Topic: {$topic}\n\n"; echo "\t$msg\n\n"; });

(As in the example, I inserted it into a controller).

Taking a look I noticed that the problem is here:

$buffer .= chr($callback["qos"]); in MqttService:175

If I solve the problem adding a simple "?? 0", when I try to subscribe, it doesn't seems to work. It enters on while($client->proc()) loop (Mqtt:114) but after a publish, I don't receive the message.

I'm using Laravel 5.8 with PHP 7.1.33 and "salmanzafar/laravel-mqtt": "^2.0".

chrisbeaver commented 2 years ago

When I receive a message on a topic I subscribe to I get this error as well.

image

muhammadaldan commented 2 years ago

How you fix this? i have same problem to

chrisbeaver commented 2 years ago

I traced through the code, and found that when you first call ConnectAndSubscribe, inside the library a call to function subscribe() is made. That Line 173 above passes because it is a single array coming in with "qos" set to zero.

Not until a message is received does it crash. And I traced through the code to find out why. Rather than an array coming in with ["qos" => 0, " "function" => $proc], it now comes back as a nested array, with key value pairs keyed with the $topic name.

I have NO CLUE AT ALL if this is CORRECTs, but it stopped breaking my code. All I did was change line 173 in Salman\Mqtt\MqttClass\MqttService to look like this. It permits the first pass, and then it permits subsequent passes in my use case.

I'm not saying it is correct, but I'm going to be working with it and testing to see if I'm getting my desired results.

$buffer .= isset($callback["qos"]) ? chr($callback["qos"]) : chr($callback[$key]["qos"]);

image

chrisbeaver commented 2 years ago

Spending a little more time on it, I went back and changed this line here to just set the initial value to an empty array.

https://github.com/salmanzafar949/MQTT-Laravel/blob/master/src/MqttClass/Mqtt.php#L105

However, the code in this repo is all just an older copy and paste of Bluerhinos PHP MQTT library. So I built my own that pulls in the current Bluerhinos library using Composer since I ran into additional issues that appear to have been fixed in Bluerhino's updates.