PHPackages                             anviz/php-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. anviz/php-mqtt-client

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

anviz/php-mqtt-client
=====================

Anviz MQTT Client

1.0.0(5y ago)012Apache-2.0PHPPHP ^7.2

Since Jul 10Pushed 5y ago1 watchersCompare

[ Source](https://github.com/AnvizJacobs/php-mqtt-client)[ Packagist](https://packagist.org/packages/anviz/php-mqtt-client)[ RSS](/packages/anviz-php-mqtt-client/feed)WikiDiscussions master Synced 3mo ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (0)

anviz/php-mqtt-client
=====================

[](#anvizphp-mqtt-client)

[![Latest Stable Version](https://camo.githubusercontent.com/2ac477ca52d65544d446f724b0d1ec990d46fc0305feedb79ffc9c80d4ba0854/68747470733a2f2f706f7365722e707567782e6f72672f7068702d6d7174742f636c69656e742f76)](//packagist.org/packages/anviz/php-mqtt-client)[![Total Downloads](https://camo.githubusercontent.com/6bde9a3ad2c1b4f409e3e1364f3f61e3fc2be5f2a646a57d7f5b2e71681d753b/68747470733a2f2f706f7365722e707567782e6f72672f7068702d6d7174742f636c69656e742f646f776e6c6f616473)](//packagist.org/packages/anviz/php-mqtt-client)[![License](https://camo.githubusercontent.com/99a12cf961e6136ccbe5f489e984ef5ba4f34325a3e31f1f205d3c98ee93c64a/68747470733a2f2f706f7365722e707567782e6f72672f7068702d6d7174742f636c69656e742f6c6963656e7365)](//packagist.org/packages/anviz/php-mqtt-client)

[`php-mqtt/client`](https://packagist.org/packages/anviz/php-mqtt-client) was created by, and is maintained by [Jacobs](https://github.com/AnvizJacobs). It allows you to connect to an MQTT broker where you can publish messages and subscribe to topics. The implementation supports all QoS levels ([with limitations](#limitations)

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

[](#installation)

```
composer require anviz/mqttclient
```

This library requires PHP version 7.2 or higher.

Usage
-----

[](#usage)

#### Publish

[](#publish)

A very basic publish example requires only three steps: connect, publish and close

```
$clientId = 'test-publisher';

$mqtt = new MQTTClient($server, $port, $clientId);
$mqtt->connect();
$mqtt->publish('php-mqtt/client/test', 'Hello World!', 0);
$mqtt->close();
```

If you do not want to pass a `$clientId`, a random one will be generated for you. This will basically force a clean session implicitly.

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

#### Subscribe

[](#subscribe)

Subscribing is a little more complex than publishing as it requires to run an event loop:

```
$clientId = 'test-subscriber';

$mqtt = new MQTTClient($server, $port, $clientId);
$mqtt->connect();
$mqtt->subscribe('php-mqtt/client/test', function ($topic, $message) {
    echo sprintf("Received message on topic [%s]: %s\n", $topic, $message);
}, 0);
$mqtt->loop(true);
```

While the loop is active, you can use `$mqtt->interrupt()` to send an interrupt signal to the loop. This will terminate the loop before it starts its next iteration. You can call this method using `pcntl_signal(SIGINT, $handler)` for example:

```
pcntl_async_signals(true);

$clientId = 'test-subscriber';

$mqtt = new MQTTClient($server, $port, $clientId);
pcntl_signal(SIGINT, function (int $signal, $info) use ($mqtt) {
    $mqtt->interrupt();
});
$mqtt->connect();
$mqtt->subscribe('php-mqtt/client/test', function ($topic, $message) {
    echo sprintf("Received message on topic [%s]: %s\n", $topic, $message);
}, 0);
$mqtt->loop(true);
$mqtt->close();
```

Features
--------

[](#features)

- MQTT Versions
    - v3.1
    - v3.1.1
    - v5.0
- Transport
    - TCP (unsecured)
    - TLS (secured, verifies the peer using a certificate authority file)
- Connect
    - Last Will
    - Last Will Topic
    - Last Will Message
    - Required QoS
    - Message Retention
    - Authentication (username &amp; password)
    - Clean Session (can be set and sent, but the client has no persistence for QoS 2 messages)
- Publish
    - QoS Level 0
    - QoS Level 1 (limitation: no persisted state across sessions)
    - QoS Level 2 (limitation: no persisted state across sessions)
- Subscribe
    - QoS Level 0
    - QoS Level 1
    - QoS Level 2 (limitation: no persisted state across sessions)
- Supported Message Length: unlimited *(no limits enforced, although the MQTT protocol supports only up to 256MB which one shouldn't use even remotely anyway)*
- Logging possible (`Psr\Log\LoggerInterface` can be passed to the client)
- Persistence Drivers
    - In-Memory Driver
    - Redis Driver

Limitations
-----------

[](#limitations)

- There is no guarantee that message identifiers are not used twice (while the first usage is still pending). The current implementation uses a simple counter which resets after all 65535 identifiers were used. This means that as long as the client isn't used to an extent where acknowledgements are open for a very long time, you should be fine. This also only affects QoS levels higher than 0, as QoS level 0 is a simple fire and forget mode.
- Message flows with a QoS level higher than 0 are not persisted as the default implementation uses an in-memory repository for data. To avoid issues with broken message flows, use the clean session flag to indicate that you don't care about old data.

License
-------

[](#license)

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

###  Health Score

22

—

LowBetter than 21% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

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.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

2185d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/eb585ebb750764388f74d44ff8a071b594af9f6441d1a6cb925211e34078452e?d=identicon)[anviz](/maintainers/anviz)

---

Tags

clientpublishsubscribemqtt

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/anviz-php-mqtt-client/health.svg)

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

###  Alternatives

[symfony/lock

Creates and manages locks, a mechanism to provide exclusive access to a shared resource

514139.2M692](/packages/symfony-lock)[php-mqtt/client

An MQTT client written in and for PHP.

4542.4M36](/packages/php-mqtt-client)[matomo/matomo

Matomo is the leading Free/Libre open analytics platform

21.7k38.9k](/packages/matomo-matomo)[php-mqtt/laravel-client

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

234627.4k3](/packages/php-mqtt-laravel-client)[ecotone/ecotone

Enterprise architecture layer for Laravel and Symfony — CQRS, Event Sourcing, Durable Workflows (Sagas, Orchestrators), Projections, and Outbox messaging via PHP attributes.

564576.7k53](/packages/ecotone-ecotone)[civicrm/civicrm-core

Open source constituent relationship management for non-profits, NGOs and advocacy organizations.

751291.4k43](/packages/civicrm-civicrm-core)

PHPackages © 2026

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