Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php MQTT subscribe not work

Tags:

php

mqtt

phpmqtt

I have installed MQTT broker on my windows machine from https://mosquitto.org/download/.

I am using https://github.com/bluerhinos/phpMQTT/tree/master/examples for publish and subscribe. Publish works as expected but subscribe did not work.

I did not understand why this is not work. MQTT broker require for connect remote MQTT server ? I want to implement publish and subscribe method in php

Publish:

require("../phpMQTT.php");


$mqtt = new phpMQTT("host", 1883, "124464646464/32"); //Change client name to something unique

if ($mqtt->connect()) {
    echo "Connect Sucssfully";
    try
    {
        $mqtt->publish("124464646464/Test","Hello HK123! at ".date("r"),0);
        echo  "<br>publish done";
        $mqtt->close();
    }
    catch(Exception $e)
    {
        echo $e;
    }
}

Subscribe:

ini_set('output_buffering','on');
require("../phpMQTT.php");


$mqtt = new phpMQTT("host", 1883, "124464646464/27"); //Change client name to something unique

if(!$mqtt->connect()){
    exit(1);
}

$topics['124464646464/Test'] = array("qos"=>0, "function"=>"procmsg");
$mqtt->subscribe($topics,0);

while($mqtt->proc()){

}


$mqtt->close();

function procmsg($topic,$msg){
        echo "Msg Recieved: ".date("r")."\nTopic:{$topic}\n$msg\n";
}

Error: wrong subscribe header

Get error form host error log

Edit:

I have run subscribe script using command line and it's always print "eof receive going to reconnect for good measure\n" means always get true from feof function

code:

if(feof($this->socket)){
                if($this->debug) echo "eof receive going to reconnect for good measure\n";
                fclose($this->socket);
                $this->connect_auto(false);
                if(count($this->topics))
                    $this->subscribe($this->topics);    
            } 

I have tested my publish page using chrome extension means subscribe using chrome extension and it's working fine means something in wrong in read socket .

like image 453
Hkachhia Avatar asked Nov 09 '22 10:11

Hkachhia


1 Answers

Update the phpMQTT.php file:

private $socket=8080;           /* holds the socket */
like image 127
braj raj Avatar answered Nov 14 '22 22:11

braj raj