PHPackages                             basttyy/laravel-mqtt-client - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. basttyy/laravel-mqtt-client

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

basttyy/laravel-mqtt-client
===========================

A wrapper for the basttyy/php-mqtt library for Laravel.

0209PHP

Since Apr 23Pushed 4y ago1 watchersCompare

[ Source](https://github.com/Basttyy/laravel-mqtt)[ Packagist](https://packagist.org/packages/basttyy/laravel-mqtt-client)[ RSS](/packages/basttyy-laravel-mqtt-client/feed)WikiDiscussions main Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

php-mqtt/laravel-client
=======================

[](#php-mqttlaravel-client)

[![Latest Stable Version](https://camo.githubusercontent.com/95381f7401fe3656c0caff0ef68cc36dd5a00b57a534ade4deb6d259db385c42/68747470733a2f2f706f7365722e707567782e6f72672f7068702d6d7174742f6c61726176656c2d636c69656e742f76)](https://packagist.org/packages/php-mqtt/laravel-client)[![Total Downloads](https://camo.githubusercontent.com/33748ddd82cee664aac1467c75df1706a26a8385157a707f776920e637657b3e/68747470733a2f2f706f7365722e707567782e6f72672f7068702d6d7174742f6c61726176656c2d636c69656e742f646f776e6c6f616473)](https://packagist.org/packages/php-mqtt/laravel-client)[![Tests](https://github.com/php-mqtt/laravel-client/workflows/Tests/badge.svg)](https://github.com/php-mqtt/laravel-client/actions?query=workflow%3ATests)[![Quality Gate Status](https://camo.githubusercontent.com/edc802872e14128f64bb627c69ef588b917efe0bd6c2bfb225045cdd84bebe24/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d7068702d6d7174745f6c61726176656c2d636c69656e74266d65747269633d616c6572745f737461747573)](https://sonarcloud.io/dashboard?id=php-mqtt_laravel-client)[![Maintainability Rating](https://camo.githubusercontent.com/b41358254eed1e680105d277a2def94600112d261fda315a54e4ae2e2a26dc85/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d7068702d6d7174745f6c61726176656c2d636c69656e74266d65747269633d7371616c655f726174696e67)](https://sonarcloud.io/dashboard?id=php-mqtt_laravel-client)[![Reliability Rating](https://camo.githubusercontent.com/65d1897899278cb52d9917aeb4729657ee95b9505dbba7d01ae93178eefdf8d7/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d7068702d6d7174745f6c61726176656c2d636c69656e74266d65747269633d72656c696162696c6974795f726174696e67)](https://sonarcloud.io/dashboard?id=php-mqtt_laravel-client)[![Security Rating](https://camo.githubusercontent.com/84e1195861d10beaea96379fb02929b852ef8bc748cce5c5cc03728b59e14936/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d7068702d6d7174745f6c61726176656c2d636c69656e74266d65747269633d73656375726974795f726174696e67)](https://sonarcloud.io/dashboard?id=php-mqtt_laravel-client)[![Vulnerabilities](https://camo.githubusercontent.com/fd358616b5c2b72eca5e72a323874faf004479abcc920a4112d5d33d7049eedc/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d7068702d6d7174745f6c61726176656c2d636c69656e74266d65747269633d76756c6e65726162696c6974696573)](https://sonarcloud.io/dashboard?id=php-mqtt_laravel-client)[![License](https://camo.githubusercontent.com/4d4f7355cc1f937ac3fe5c39d2fb129cca7a8a8e032e84851ad4279edd01cd1a/68747470733a2f2f706f7365722e707567782e6f72672f7068702d6d7174742f6c61726176656c2d636c69656e742f6c6963656e7365)](https://packagist.org/packages/php-mqtt/laravel-client)

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

Installation
------------

[](#installation)

The package is available on [packagist.org](https://packagist.org/packages/php-mqtt/laravel-client) and can be installed using composer:

```
composer require php-mqtt/laravel-client
```

The package will register itself through Laravel auto discovery of packages. Registered will be the service provider as well as an `MQTT` facade.

After installing the package, you should publish the configuration file using

```
php artisan vendor:publish --provider="PhpMqtt\Client\MqttClientServiceProvider" --tag="config"
```

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

Configuration
-------------

[](#configuration)

The package allows you to configure multiple named connections. An initial example with inline documentation can be found in the published configuration file. Most of the configuration options are optional and come with sane defaults (especially all of the `connection_settings`).

An example configuration of two connections, where one is meant for sharing public data and one for private data, could look like the following:

```
'default_connection' => 'private',

'connections' => [
    'private' => [
        'host' => 'mqtt.example.com',
        'port' => 1883,
    ],
    'public' => [
        'host' => 'test.mosquitto.org',
        'port' => 1883,
    ],
],
```

In this example, the private connection is the default one.

*Note: it is recommended to use environment variables to configure the MQTT client. Available environment variables can be found in the configuration file.*

Usage
-----

[](#usage)

### Publish (QoS level 0)

[](#publish-qos-level-0)

Publishing a message with QoS level 0 is quite easy and can be done in a single command:

```
use PhpMqtt\Client\Facades\MQTT;

MQTT::publish('some/topic', 'Hello World!');
```

If needed, the *retain* flag (default: `false`) can be passed as third and the connection name as fourth parameter:

```
use PhpMqtt\Client\Facades\MQTT;

MQTT::publish('some/topic', 'Hello World!', true, 'public');
```

Using `MQTT::publish($topic, $message)` will implicitly call `MQTT::connection()`, but the connection will not be closed after usage. If you want to close the connection manually because your script does not need the connection anymore, you can call `MQTT:disconnect()` (optionally with the connection name as a parameter). Please also note that using `MQTT::publish($topic, $message)` will always use QoS level 0. If you need a different QoS level, you will need to use the `MqttClient` directly which is explained below.

### Publish (QoS level 1 &amp; 2)

[](#publish-qos-level-1--2)

Different to QoS level 0, we need to run an event loop in order for QoS 1 and 2 to work. This is because with a one-off command we cannot guarantee that a message reaches its target. The event loop will ensure a published message gets sent again if no acknowledgment is returned by the broker within a grace period. Only when the broker returns an acknowledgement (or all of the acknowledgements in case of QoS 2), the client will stop resending the message.

```
use PhpMqtt\Client\Facades\MQTT;

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

`$mqtt->loop()` actually starts an infinite loop. To escape it, there are multiple options. In case of simply publishing a message, all we want is to receive an acknowledgement. Therefore, we can simply pass `true` as second parameter to exit the loop as soon as all resend queues are cleared:

```
/** @var \PhpMqtt\Client\Contracts\MqttClient $mqtt */
$mqtt->loop(true, true);
```

In order to escape the loop, you can also call `$mqtt->interrupt()` which will exit the loop during the next iteration. The method can, for example, be called in a registered signal handler:

```
/** @var \PhpMqtt\Client\Contracts\MqttClient $mqtt */
pcntl_signal(SIGINT, function () use ($mqtt) {
    $mqtt->interrupt();
});
```

### Subscribe

[](#subscribe)

Very similar to publishing with QoS level 1 and 2, subscribing requires to run an event loop. Although before running the loop, topics need to be subscribed to:

```
use PhpMqtt\Client\Facades\MQTT;

/** @var \PhpMqtt\Client\Contracts\MqttClient $mqtt */
$mqtt = MQTT::connection();
$mqtt->subscribe('some/topic', function (string $topic, string $message) {
    echo sprintf('Received QoS level 1 message on topic [%s]: %s', $topic, $message);
}, 1);
$mqtt->loop(true);
```

Features
--------

[](#features)

This library allows you to use all the features provided by [`php-mqtt/client`](https://github.com/php-mqtt/client). Simply retrieve an instance of `\PhpMqtt\Client\Contracts\MqttClient` with `MQTT::connection(string $name = null)` and use it directly.

For an extensive collection of examples which explain how to use the MQTT client (directly), you can visit the [`php-mqtt/client-examples` repository](https://github.com/php-mqtt/client-examples).

License
-------

[](#license)

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

###  Health Score

17

—

LowBetter than 6% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity27

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/23058396?v=4)[Abdulbasit Mamman](/maintainers/basttyy)[@Basttyy](https://github.com/Basttyy)

---

Top Contributors

[![Basttyy](https://avatars.githubusercontent.com/u/23058396?v=4)](https://github.com/Basttyy "Basttyy (2 commits)")

### Embed Badge

![Health badge](/badges/basttyy-laravel-mqtt-client/health.svg)

```
[![Health](https://phpackages.com/badges/basttyy-laravel-mqtt-client/health.svg)](https://phpackages.com/packages/basttyy-laravel-mqtt-client)
```

###  Alternatives

[fergusean/nusoap

NuSOAP re-packaged for Packagist/Composer

281.4M10](/packages/fergusean-nusoap)[tripal/tripal

Tripal is a toolkit to facilitate construction of online genomic, genetic (and other biological) websites.

7012.3k9](/packages/tripal-tripal)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
