Skip to content

Commit 78af7a1

Browse files
authored
Update the package for v1 of the MQTT client (#1)
* Update composer packages and untrack composer.lock * Update code and config for v1.0.0-rc1 * Use contract instead of implementation * Update README.md
1 parent 2ea0c4c commit 78af7a1

9 files changed

+198
-792
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/.idea/
22
/vendor/
3+
composer.lock

README.md

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
# php-mqtt/laravel-client
22

3-
**:warning: Work in progress - use at own risk! :warning:**
3+
[![Latest Stable Version](https://poser.pugx.org/php-mqtt/laravel-client/v)](https://packagist.org/packages/php-mqtt/laravel-client)
4+
[![Total Downloads](https://poser.pugx.org/php-mqtt/laravel-client/downloads)](https://packagist.org/packages/php-mqtt/laravel-client)
5+
[![License](https://poser.pugx.org/php-mqtt/laravel-client/license)](https://packagist.org/packages/php-mqtt/laravel-client)
46

5-
`php-mqtt/laravel-client` was created by, and is maintained by [Namoshek](https://github.com/namoshek).
7+
`php-mqtt/laravel-client` was created by, and is maintained by [Marvin Mall](https://github.com/namoshek).
68
It is a Laravel wrapper for the [`php-mqtt/client`](https://github.com/php-mqtt/client) package and
79
allows you to connect to an MQTT broker where you can publish messages and subscribe to topics.
810

911
## Installation
1012

13+
The package is available on [packagist.org](https://packagist.org/packages/php-mqtt/laravel-client) and can be installed using composer:
14+
1115
```bash
1216
composer require php-mqtt/laravel-client
1317
```
@@ -18,20 +22,17 @@ Registered will be the service provider as well as an `MQTT` facade.
1822
After installing the package, you should publish the configuration file using
1923

2024
```bash
21-
php artisan vendor:publish --provider="PhpMqtt\Client\MqttClientServiceProvider"
25+
php artisan vendor:publish --provider="PhpMqtt\Client\MqttClientServiceProvider" --tag="config"
2226
```
2327

2428
and change the configuration in `config/mqtt-client.php` according to your needs.
2529

2630
## Configuration
2731

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+
The package allows you to configure multiple named connections. An initial example with inline documentation can be found in the published configuration file.
33+
Most of the configuration options are optional and come with sane defaults (especially all of the `connection_settings`).
3234

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+
An example configuration of two connections, where one is meant for sharing public data and one for private data, could look like the following:
3536
```php
3637
'default_connection' => 'private',
3738

@@ -48,6 +49,8 @@ data and one for private data, could look like the following:
4849
```
4950
In this example, the private connection is the default one.
5051

52+
_Note: it is recommended to use environment variables to configure the MQTT client. Available environment variables can be found in the configuration file._
53+
5154
## Usage
5255

5356
### Publish (QoS level 0)
@@ -59,53 +62,64 @@ use PhpMqtt\Client\Facades\MQTT;
5962
MQTT::publish('some/topic', 'Hello World!');
6063
```
6164

62-
If needed, the connection name can be passed as third parameter:
65+
If needed, the _retain_ flag (default: `false`) can be passed as third and the connection name as fourth parameter:
6366
```php
6467
use PhpMqtt\Client\Facades\MQTT;
6568

66-
MQTT::publish('some/topic', 'Hello World!', 'public');
69+
MQTT::publish('some/topic', 'Hello World!', true, 'public');
6770
```
6871

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).
72+
Using `MQTT::publish($topic, $message)` will implicitly call `MQTT::connection()`, but the connection will not be closed after usage.
73+
If you want to close the connection manually because your script does not need the connection anymore,
74+
you can call `MQTT:disconnect()` (optionally with the connection name as a parameter).
7375
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.
76+
If you need a different QoS level, you will need to use the `MqttClient` directly which is explained below.
7577

7678
### Publish (QoS level 1 & 2)
7779

7880
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.
81+
This is because with a one-off command we cannot guarantee that a message reaches its target.
82+
The event loop will ensure a published message gets sent again if no acknowledgment is returned by the broker within a grace period.
83+
Only when the broker returns an acknowledgement (or all of the acknowledgements in case of QoS 2),
84+
the client will stop resending the message.
8385

8486
```php
8587
use PhpMqtt\Client\Facades\MQTT;
8688

89+
/** @var \PhpMqtt\Client\Contracts\MqttClient $mqtt */
8790
$mqtt = MQTT::connection();
8891
$mqtt->publish('some/topic', 'foo', 1);
89-
$mqtt->publish('some/other/topic', 'bar', 2);
92+
$mqtt->publish('some/other/topic', 'bar', 2, true); // Retain the message
9093
$mqtt->loop(true);
9194
```
9295

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:
96+
`$mqtt->loop()` actually starts an infinite loop. To escape it, there are multiple options.
97+
In case of simply publishing a message, all we want is to receive an acknowledgement.
98+
Therefore, we can simply pass `true` as second parameter to exit the loop as soon as all resend queues are cleared:
99+
95100
```php
96-
pcntl_signal(SIGINT, function (int $signal, $info) use ($mqtt) {
101+
/** @var \PhpMqtt\Client\Contracts\MqttClient $mqtt */
102+
$mqtt->loop(true, true);
103+
```
104+
105+
In order to escape the loop, you can also call `$mqtt->interrupt()` which will exit the loop during
106+
the next iteration. The method can, for example, be called in a registered signal handler:
107+
```php
108+
/** @var \PhpMqtt\Client\Contracts\MqttClient $mqtt */
109+
pcntl_signal(SIGINT, function () use ($mqtt) {
97110
$mqtt->interrupt();
98111
});
99112
```
100113

101114
### Subscribe
102115

103116
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:
117+
Although before running the loop, topics need to be subscribed to:
105118

106119
```php
107120
use PhpMqtt\Client\Facades\MQTT;
108121

122+
/** @var \PhpMqtt\Client\Contracts\MqttClient $mqtt */
109123
$mqtt = MQTT::connection();
110124
$mqtt->subscribe('some/topic', function (string $topic, string $message) {
111125
echo sprintf('Received QoS level 1 message on topic [%s]: %s', $topic, $message);
@@ -116,7 +130,11 @@ $mqtt->loop(true);
116130
## Features
117131

118132
This library allows you to use all the features provided by [`php-mqtt/client`](https://github.com/php-mqtt/client).
133+
Simply retrieve an instance of `\PhpMqtt\Client\Contracts\MqttClient` with `MQTT::connection(string $name = null)` and use it directly.
134+
135+
For an extensive collection of examples which explain how to use the MQTT client (directly),
136+
you can visit the [`php-mqtt/client-examples` repository](https://github.com/php-mqtt/client-examples).
119137

120138
## License
121139

122-
`php-mqtt/laravel-client` is open-sourced software licensed under the [MIT license](LICENSE.md).
140+
`php-mqtt/laravel-client` is open-source software licensed under the [MIT license](LICENSE.md).

composer.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "php-mqtt/laravel-client",
3-
"description": "An MQTT client library for Laravel.",
3+
"description": "A wrapper for the php-mqtt/client library for Laravel.",
44
"type": "library",
55
"keywords": [
66
"mqtt",
@@ -13,16 +13,16 @@
1313
"license": "MIT",
1414
"authors": [
1515
{
16-
"name": "Namoshek",
17-
"email": "namoshek@gmx.at",
16+
"name": "Marvin Mall",
17+
"email": "marvin-mall@msn.com",
1818
"role": "developer"
1919
}
2020
],
2121
"require": {
22-
"php": "^7.2",
23-
"illuminate/config": "~5.8|~6.0",
24-
"illuminate/support": "~5.8|~6.0",
25-
"php-mqtt/client": "0.1.*"
22+
"php": "^7.4|^8.0",
23+
"illuminate/config": "~7.0|~8.0",
24+
"illuminate/support": "~7.0|~8.0",
25+
"php-mqtt/client": "v1.0.0-rc1"
2626
},
2727
"autoload": {
2828
"psr-4": {

0 commit comments

Comments
 (0)