From 7148ef72289b823f6d5c4ef8c8d01ffdfb76cf23 Mon Sep 17 00:00:00 2001 From: Salman Zafar Date: Tue, 29 Oct 2019 17:01:02 +0500 Subject: [PATCH] added qos and retan flags for publishing and subsrcbe methods --- Readme.md | 4 ++++ src/MqttClass/Mqtt.php | 8 ++++++-- src/config/mqtt.php | 8 +++++--- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Readme.md b/Readme.md index 0672c91..dfc3aae 100644 --- a/Readme.md +++ b/Readme.md @@ -16,6 +16,8 @@ composer require salmanzafar/laravel-mqtt * Certificate Protection for end to end encryption * Enable Debug mode to make it easier for debugging * Now you can also set Client_id of your choice and if you don't want just simply don't use or set it to null +* Set QOS flag directly from config file +* Set Retain flag directly from config file ## Enable the package (Optional) @@ -51,6 +53,8 @@ php artisan vendor:publish --provider="Salman\Mqtt\MqttServiceProvider" 'certfile' => env('mqtt_cert_file',''), 'port' => env('mqtt_port','1883'), 'debug' => env('mqtt_debug',false) //Optional Parameter to enable debugging set it to True + 'qos' => env('mqtt_qos', 0), // set quality of service here + 'retain' => env('mqtt_retain', 0) // it should be 0 or 1 Whether the message should be retained.- Retain Flag ``` #### Publishing topic diff --git a/src/MqttClass/Mqtt.php b/src/MqttClass/Mqtt.php index 6309fe2..1aa2374 100644 --- a/src/MqttClass/Mqtt.php +++ b/src/MqttClass/Mqtt.php @@ -40,6 +40,8 @@ class Mqtt protected $password = null; protected $port = null; protected $debug = null; + protected $qos = 0; + protected $retain = 0; public function __construct() { @@ -49,6 +51,8 @@ class Mqtt $this->cert_file = config('mqtt.certfile'); $this->port = config('mqtt.port'); $this->debug = config('mqtt.debug'); + $this->qos = config('mqtt.qos'); + $this->retain = config('mqtt.retain'); } @@ -61,7 +65,7 @@ class Mqtt if ($client->connect(true, null, $this->username, $this->password)) { - $client->publish($topic,$msg); + $client->publish($topic,$msg, $this->qos, $this->retain); $client->close(); return true; @@ -81,7 +85,7 @@ class Mqtt { $topics[$topic] = array("qos" => 0, "function" => $proc); - $client->subscribe($topics, 0); + $client->subscribe($topics, $this->qos); while($client->proc()) { diff --git a/src/config/mqtt.php b/src/config/mqtt.php index bf31245..34d14ee 100644 --- a/src/config/mqtt.php +++ b/src/config/mqtt.php @@ -8,10 +8,12 @@ return [ - 'host' => env('mqtt_host','127.0.0.1'), + 'host' => env('mqtt_host','127.0.0.1'), 'password' => env('mqtt_password',''), 'username' => env('mqtt_username',''), 'certfile' => env('mqtt_cert_file',''), - 'port' => env('mqtt_port','1883'), - 'debug' => env('mqtt_debug',false) //Optional Parameter to enable debugging set it to True + 'port' => env('mqtt_port','1883'), + 'debug' => env('mqtt_debug',false), //Optional Parameter to enable debugging set it to True + 'qos' => env('mqtt_qos', 0), // set quality of service here + 'retain' => env('mqtt_retain', 0) // it should be 0 or 1 Whether the message should be retained.- Retain Flag ];