added qos and retan flags for publishing and subsrcbe methods

This commit is contained in:
Salman Zafar 2019-10-29 17:01:02 +05:00
parent 7911a8cb6a
commit 7148ef7228
3 changed files with 15 additions and 5 deletions

View File

@ -16,6 +16,8 @@ composer require salmanzafar/laravel-mqtt
* Certificate Protection for end to end encryption * Certificate Protection for end to end encryption
* Enable Debug mode to make it easier for debugging * 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 * 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) ## Enable the package (Optional)
@ -51,6 +53,8 @@ php artisan vendor:publish --provider="Salman\Mqtt\MqttServiceProvider"
'certfile' => env('mqtt_cert_file',''), 'certfile' => env('mqtt_cert_file',''),
'port' => env('mqtt_port','1883'), 'port' => env('mqtt_port','1883'),
'debug' => env('mqtt_debug',false) //Optional Parameter to enable debugging set it to True '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 #### Publishing topic

View File

@ -40,6 +40,8 @@ class Mqtt
protected $password = null; protected $password = null;
protected $port = null; protected $port = null;
protected $debug = null; protected $debug = null;
protected $qos = 0;
protected $retain = 0;
public function __construct() public function __construct()
{ {
@ -49,6 +51,8 @@ class Mqtt
$this->cert_file = config('mqtt.certfile'); $this->cert_file = config('mqtt.certfile');
$this->port = config('mqtt.port'); $this->port = config('mqtt.port');
$this->debug = config('mqtt.debug'); $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)) if ($client->connect(true, null, $this->username, $this->password))
{ {
$client->publish($topic,$msg); $client->publish($topic,$msg, $this->qos, $this->retain);
$client->close(); $client->close();
return true; return true;
@ -81,7 +85,7 @@ class Mqtt
{ {
$topics[$topic] = array("qos" => 0, "function" => $proc); $topics[$topic] = array("qos" => 0, "function" => $proc);
$client->subscribe($topics, 0); $client->subscribe($topics, $this->qos);
while($client->proc()) while($client->proc())
{ {

View File

@ -8,10 +8,12 @@
return [ return [
'host' => env('mqtt_host','127.0.0.1'), 'host' => env('mqtt_host','127.0.0.1'),
'password' => env('mqtt_password',''), 'password' => env('mqtt_password',''),
'username' => env('mqtt_username',''), 'username' => env('mqtt_username',''),
'certfile' => env('mqtt_cert_file',''), 'certfile' => env('mqtt_cert_file',''),
'port' => env('mqtt_port','1883'), 'port' => env('mqtt_port','1883'),
'debug' => env('mqtt_debug',false) //Optional Parameter to enable debugging set it to True '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
]; ];