Rabbit MQTT client in PHP?

◇◆丶佛笑我妖孽 提交于 2019-12-08 06:45:57

问题


I am new to MQTT. Can anyone help how to use Rabbitmq mqtt in PHP, I have MQTT broker in cloud so I want to develop based on PHP in my local system. Any library we want to download? Can anyone help on that in Ubuntu?


回答1:


You can use the client lib: https://github.com/bluerhinos/phpMQTT as described here: https://www.cloudamqp.com/docs/php_mqtt.html

Publisher

require("phpMQTT.php");
$host = "hostname";
$port = port;
$username = "username";
$password = "password";
$message = "Hello CloudAMQP MQTT!";

//MQTT client id to use for the device. "" will generate a client id     automatically
$mqtt = new phpMQTT($host, $port, "ClientID".rand());

if ($mqtt->connect(true,NULL,$username,$password)) {
  $mqtt->publish("topic",$message, 0);
  $mqtt->close();
}else{
  echo "Fail or time out";
}

Subscriber

require("phpMQTT.php");

$host = "hostname";
$port = port;
$username = "username";
$password = "password";

$mqtt = new phpMQTT($host, $port, "ClientID".rand());

if(!$mqtt->connect(true,NULL,$username,$password)){
  exit(1);
}

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

while($mqtt->proc()){
}

$mqtt->close();
function procmsg($topic,$msg){
  echo "Msg Recieved: $msg";
}   

MQTT is enabled by default on all CloudAMQP servers, so there is no need to enable the MQTT plugin if you are using CloudAMQP as MQTT broker. If not, you need to enable this plugin: https://www.rabbitmq.com/mqtt.html



来源:https://stackoverflow.com/questions/46092964/rabbit-mqtt-client-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!