Skip to content

Commit 525882b

Browse files
authored
Clarify need of "loop" for QoS 1 and 2 publications. (#212)
1 parent 3d14184 commit 525882b

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,23 @@ If you do not want to pass a `$clientId`, a random one will be generated for you
4949

5050
Be also aware that most of the methods can throw exceptions. The above example does not add any exception handling for brevity.
5151

52+
Be aware that for publications with QoS levels 1 or 2 a later call to `loop` method is required in order for the additional acknowledgement messages from the broker to be correctly processed. Not doing so will avoid QoS 2 from being delivered at all and QoS 1 messages might or might not arrive depending on specific broker implementation details.
53+
54+
An example:
55+
56+
```php
57+
$server = 'some-broker.example.com';
58+
$port = 1883;
59+
$clientId = 'test-publisher';
60+
61+
$mqtt = new \PhpMqtt\Client\MqttClient($server, $port, $clientId);
62+
$mqtt->connect();
63+
$mqtt->publish('php-mqtt/client/test', 'Hello Exactly Once World!', 2);
64+
// You can set a third optional parameter as a timeout
65+
$mqtt->loop(true, true);
66+
$mqtt->disconnect();
67+
```
68+
5269
### Subscribe
5370

5471
Subscribing is a little more complex than publishing as it requires to run an event loop which reads, parses and handles messages from the broker:
@@ -96,6 +113,7 @@ A fifth parameter allows passing a repository (currently, only a `MemoryReposito
96113
Lastly, a logger can be passed as sixth parameter. If none is given, a null logger is used instead.
97114

98115
Example:
116+
99117
```php
100118
$mqtt = new \PhpMqtt\Client\MqttClient(
101119
$server,
@@ -112,10 +130,12 @@ The `Logger` must implement the `Psr\Log\LoggerInterface`.
112130
### Connection Settings
113131

114132
The `connect()` method of the `MqttClient` takes two optional parameters:
133+
115134
1. A `ConnectionSettings` instance
116135
2. A `boolean` flag indicating whether a clean session should be requested (a random client id does this implicitly)
117136

118137
Example:
138+
119139
```php
120140
$mqtt = new \PhpMqtt\Client\MqttClient($server, $port, $clientId);
121141

@@ -395,14 +415,17 @@ $mqtt->unregisterConnectedEventHandler(); // Unregister all event handlers
395415
### Certificates (TLS)
396416

397417
To run the tests (especially the TLS tests), you will need to create certificates. A command has been provided for this:
418+
398419
```sh
399420
sh create-certificates.sh
400421
```
422+
401423
This will create all required certificates in the `.ci/tls/` directory. The same script is used for continuous integration as well.
402424

403425
### MQTT Broker for Testing
404426

405427
Running the tests expects an MQTT broker to be running. The easiest way to run an MQTT broker is through Docker:
428+
406429
```sh
407430
docker run --rm -it \
408431
-p 1883:1883 \
@@ -414,6 +437,7 @@ docker run --rm -it \
414437
-v $(pwd)/.ci/mosquitto.passwd:/mosquitto/config/mosquitto.passwd \
415438
eclipse-mosquitto:1.6
416439
```
440+
417441
When run from the project directory, this will spawn a Mosquitto MQTT broker configured with the generated TLS certificates and a custom configuration.
418442

419443
In case you intend to run a different broker or using a different method, or use a public broker instead,

0 commit comments

Comments
 (0)