mohan
mohan

Reputation: 11

Rabbit MQTT client in PHP?

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?

Upvotes: 1

Views: 3642

Answers (1)

Lovisa Johansson
Lovisa Johansson

Reputation: 10528

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

Upvotes: 0

Related Questions