PHP code example of nailfor / php-queue-client
1. Go to this page and download the library: Download nailfor/php-queue-client library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once ('vendor/autoload.php' );
nailfor / php-queue-client example snippets
use nailfor \queue \ClientFactory ;
use nailfor \queue \protocol \MQTT ;
class MQTTClass
{
public function subscribe ()
{
$url = '127.0.0.1:5672' ;
$options = [
'clientId' => 'php' ,
'cleanSession' => false ,
'topics' => [
'topic_name' => [
'qos' => 1 ,
'message' => 'hello from topic' ,
],
'capital_name' => [
'qos' => 2 ,
'message' => 'hello from capital' ,
],
],
];
$protocol = new MQTT;
ClientFactory::publish($url, $options, [$this , 'onError' ], $protocol);
}
}
use nailfor \queue \ClientFactory ;
use nailfor \queue \protocol \MQTT ;
use Illuminate \Support \Facades \Log ;
class MQTTClass
{
public function onMessage ($packet)
{
}
public function onCapitalMessage ($packet)
{
}
public function onError ($reason) {
echo $reason->getMessage(). PHP_EOL;
exit ;
}
public function subscribe ()
{
$url = '127.0.0.1:5672' ;
$options = [
'clientId' => 'php' ,
'cleanSession' => false ,
'topics' => [
'topic_name' => [
'qos' => 1 ,
'events' => [
'PUBLISH' => [$this , 'onMessage' ],
],
],
'capital_name' => [
'qos' => 0 ,
'events' => [
'PUBLISH' => [$this , 'onCapitalMessage' ],
],
],
],
];
$protocol = new MQTT;
$logger = Log::channel('stderr' );
ClientFactory::run($url, $options, [$this , 'onError' ], $protocol, $logger);
}
}