@@ -12,15 +12,106 @@ allows you to connect to an MQTT broker where you can publish messages and subsc
1212composer require php-mqtt/laravel-client
1313```
1414
15- You should then publish the configuration file using
15+ The package will register itself through Laravel auto discovery of packages.
16+ Registered will be the service provider as well as an ` MQTT ` facade.
17+
18+ After installing the package, you should publish the configuration file using
1619
1720``` bash
1821php artisan vendor:publish --provider=" PhpMqtt\Client\MqttClientServiceProvider"
1922```
2023
24+ and change the configuration in ` config/mqtt-client.php ` according to your needs.
25+
26+ ## Configuration
27+
28+ The package allows you to configure multiple named connections. An initial example
29+ can be found in the published configuration file. Except for the ` host ` parameter,
30+ all configuration options are entirely optional and come with the defaults provided
31+ to the ` env() ` helper in the example configuration file (no default means ` null ` ).
32+
33+ An example configuration of two connections, where one is meant for sharing public
34+ data and one for private data, could look like the following:
35+ ``` php
36+ 'default_connection' => 'private',
37+
38+ 'connections' => [
39+ 'private' => [
40+ 'host' => 'mqtt.example.com',
41+ 'port' => 1883,
42+ ],
43+ 'public' => [
44+ 'host' => 'test.mosquitto.org',
45+ 'port' => 1883,
46+ ],
47+ ],
48+ ```
49+ In this example, the private connection is the default one.
50+
2151## Usage
2252
23- // TODO: usage
53+ ### Publish (QoS level 0)
54+
55+ Publishing a message with QoS level 0 is quite easy and can be done in a single command:
56+ ``` php
57+ use PhpMqtt\Client\Facades\MQTT;
58+
59+ MQTT::publish('some/topic', 'Hello World!');
60+ ```
61+
62+ If needed, the connection name can be passed as third parameter:
63+ ``` php
64+ use PhpMqtt\Client\Facades\MQTT;
65+
66+ MQTT::publish('some/topic', 'Hello World!', 'public');
67+ ```
68+
69+ Using ` MQTT::publish($topic, $message) ` will implicitly call ` MQTT::connection() ` ,
70+ but the connection will not be closed after usage. If you want to close the connection
71+ manually because your script does not need the connection any more, you can call
72+ ` MQTT:close() ` (optionally with the connection name as a parameter).
73+ Please also note that using ` MQTT::publish($topic, $message) ` will always use QoS level 0.
74+ If you need a different QoS level, then please follow the instructions below.
75+
76+ ### Publish (QoS level 1 & 2)
77+
78+ Different to QoS level 0, we need to run an event loop in order for QoS 1 and 2 to work.
79+ This is because with a one-off command we cannot guarantee that a message reaches it's target.
80+ The event loop will ensure a published message gets sent again if no acknowledgment is returned
81+ by the broker within a grace period (in case of QoS level 1). Also handled by the event loop will
82+ be the release of packages in case of QoS level 2.
83+
84+ ``` php
85+ use PhpMqtt\Client\Facades\MQTT;
86+
87+ $mqtt = MQTT::connection();
88+ $mqtt->publish('some/topic', 'foo', 1);
89+ $mqtt->publish('some/other/topic', 'bar', 2);
90+ $mqtt->loop(true);
91+ ```
92+
93+ In order to escape the loop, you can call ` $mqtt->interrupt() ` which will exit the loop during
94+ the next iteration. The method can for example be called in a registered signal handler:
95+ ``` php
96+ pcntl_signal(SIGINT, function (int $signal, $info) use ($mqtt) {
97+ $mqtt->interrupt();
98+ });
99+ ```
100+
101+ ### Subscribe
102+
103+ Very similar to publishing with QoS level 1 and 2, subscribing requires to run an event loop.
104+ But before running the loop, topics need to be subscribed to:
105+
106+ ``` php
107+ use PhpMqtt\Client\Facades\MQTT;
108+
109+ $mqtt = MQTT::connection();
110+ $mqtt->subscribe('some/topic', function (string $topic, string $message) {
111+ echo sprintf('Received QoS level 1 message on topic [%s]: %s', $topic, $message);
112+ }, 1);
113+ $mqtt->loop(true);
114+ ```
24115
25116## Features
26117
0 commit comments