You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+24Lines changed: 24 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -49,6 +49,23 @@ If you do not want to pass a `$clientId`, a random one will be generated for you
49
49
50
50
Be also aware that most of the methods can throw exceptions. The above example does not add any exception handling for brevity.
51
51
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
+
52
69
### Subscribe
53
70
54
71
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
96
113
Lastly, a logger can be passed as sixth parameter. If none is given, a null logger is used instead.
97
114
98
115
Example:
116
+
99
117
```php
100
118
$mqtt = new \PhpMqtt\Client\MqttClient(
101
119
$server,
@@ -112,10 +130,12 @@ The `Logger` must implement the `Psr\Log\LoggerInterface`.
112
130
### Connection Settings
113
131
114
132
The `connect()` method of the `MqttClient` takes two optional parameters:
133
+
115
134
1. A `ConnectionSettings` instance
116
135
2. A `boolean` flag indicating whether a clean session should be requested (a random client id does this implicitly)
117
136
118
137
Example:
138
+
119
139
```php
120
140
$mqtt = new \PhpMqtt\Client\MqttClient($server, $port, $clientId);
121
141
@@ -395,14 +415,17 @@ $mqtt->unregisterConnectedEventHandler(); // Unregister all event handlers
395
415
### Certificates (TLS)
396
416
397
417
To run the tests (especially the TLS tests), you will need to create certificates. A command has been provided for this:
418
+
398
419
```sh
399
420
sh create-certificates.sh
400
421
```
422
+
401
423
This will create all required certificates in the `.ci/tls/` directory. The same script is used for continuous integration as well.
402
424
403
425
### MQTT Broker for Testing
404
426
405
427
Running the tests expects an MQTT broker to be running. The easiest way to run an MQTT broker is through Docker:
When run from the project directory, this will spawn a Mosquitto MQTT broker configured with the generated TLS certificates and a custom configuration.
418
442
419
443
In case you intend to run a different broker or using a different method, or use a public broker instead,
0 commit comments