PHPackages                             jesusslim/mqttclient - 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. jesusslim/mqttclient

ActiveLibrary

jesusslim/mqttclient
====================

php mqtt client

1.52(6y ago)2057217[1 issues](https://github.com/jesusslim/mqttclient/issues)MITPHPPHP &gt;=5.6.0

Since Aug 7Pushed 6y ago1 watchersCompare

[ Source](https://github.com/jesusslim/mqttclient)[ Packagist](https://packagist.org/packages/jesusslim/mqttclient)[ RSS](/packages/jesusslim-mqttclient/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependencies (1)Versions (11)Used By (0)

jesusslim/mqttclient
====================

[](#jesusslimmqttclient)

PHP mqtt client

usage
-----

[](#usage)

\[English\] [Chinese](README_CN.md)

### Install

[](#install)

Install:

```
composer require jesusslim/mqttclient

```

If your composer not allowed dev-master,add this config

```
"minimum-stability": "dev"

```

into your composer.json.

### Require

[](#require)

```
swoole 2.0.8+(swoole.so)
mosquitto.so

```

### Example

[](#example)

There's MqttClient we can use.One is based on swoole(see swoole example,deprecated),another is based on mosquitto(see mosquitto example).

The difference:

- Requirements difference.
- Swoole has package-separation bug and crash bug.We should handle this exception,for example,monitor the process id and restart when is crash or receive error package.
- Swoole client can subscribe and publish in one client,use mosquitto we need two client.
- Some grammar difference between swoole and mosquitto.

#### Example base on mosquitoo

[](#example-base-on-mosquitoo)

define your logger:

```
class Logger implements \mqttclient\src\swoole\MqttLogInterface {

	public function log($type,$content,$params = []){
	        echo "$type : $content \r\n";
	 }
}

```

use Mqttclient

```
$host = '127.0.0.1';
$port = 1883;
$r = new \mqttclient\src\mosquitto\MqttClient($host,$port,10017);
$r->setAuth('username','password');
$r->setKeepAlive(60);
$r->setLogger(new Logger());
$r->setMaxReconnectTimesWhenError(360*12);
//reconnect interval
$r->setReconnectInterval(10);
//subscribe topics,callback's params can be any data we mapped into the container(IOC)
$r->setTopics(
[
    new \mqttclient\src\subscribe\Topic('test/slim',function($msg){
        echo "I receive:".$msg."\r\n";}),
    new \mqttclient\src\subscribe\Topic('test/slim3',function(\mqttclient\src\swoole\MqttClient $client,$msg){
        echo "I receive:".$msg." for slim3 \r\n";
        echo $client->getClientId();
    })
]
);
//set trigger
$r->on(\mqttclient\src\consts\ClientTriggers::SOCKET_CONNECT,function(){
    //do something
});
$r->start();

```

Sender:

```
$host = '127.0.0.1';
$port = 1883;
$r = new \mqttclient\src\mosquitto\MqttSender($host,$port,10017);
$r->setAuth('username','password');
$r->setKeepAlive(60);
$r->setLogger(new Logger());
$r->setMaxReconnectTimesWhenError(360*12);
//reconnect interval
$r->setReconnectInterval(10);
$r->setQueue(new Queue());
$r->start();

```

It need a queue implements mqttclient\\src\\mosquitto\\MqttSendingQueue,to get msg that need to be sent in loop.

#### Example base on swoole(deprecated)

[](#example-base-on-swooledeprecated)

define your logger:

```
class Logger implements \mqttclient\src\swoole\MqttLogInterface {

	public function log($type,$content,$params = []){
	        echo "$type : $content \r\n";
	 }
}

```

define your tmp store (use Redis/Memory/...)

```
class Store implements \mqttclient\src\swoole\TmpStorageInterface{

	private $data = [];

    public function set($message_type, $key, $sub_key, $data, $expire = 3600)
    {
        $this->data[$message_type][$key][$sub_key] = $data;
    }

    public function get($message_type, $key, $sub_key)
    {
        return $this->data[$message_type][$key][$sub_key];
    }

    public function delete($message_type, $key, $sub_key)
    {
        if (!isset($this->data[$message_type][$key][$sub_key])){
            echo "storage not found:$message_type $key $sub_key";
        }
        unset($this->data[$message_type][$key][$sub_key]);
    }

}

```

use MqttClient

```
$host = '127.0.0.1';
$port = 1883;

$r = new \mqttclient\src\swoole\MqttClient($host,$port,10017);
$r->setAuth('username','password');
$r->setKeepAlive(60);
$r->setLogger(new Logger());
$r->setStore(new Store());
//dns lookup
$r->setDnsLookup(true);
//buffer size
$r->setSocketBufferSize(1024*1024*5);
//reconnect times when error
$r->setMaxReconnectTimesWhenError(360*12);
//reconnect interval
$r->setReconnectInterval(10000);
//subscribe topics,callback's params can be any data we mapped into the container(IOC)
$r->setTopics(
[
    new \mqttclient\src\subscribe\Topic('test/slim',function($msg){
        echo "I receive:".$msg."\r\n";}),
    new \mqttclient\src\subscribe\Topic('test/slim3',function(\mqttclient\src\swoole\MqttClient $client,$msg){
        echo "I receive:".$msg." for slim3 \r\n";
        echo $client->getClientId();
    })
]
);

//set trigger
$r->on(\mqttclient\src\consts\ClientTriggers::RECEIVE_SUBACK,function(\mqttclient\src\swoole\MqttClient $client){
	$client->publish('slim/echo','GGXX',\mqttclient\src\consts\Qos::ONE_TIME);
});

$r->connect();
$r->publish('test/slim','test qos',2);

```

### Extends

[](#extends)

You can also use own client extends MqttClient.

Example:

```
class Client extends MqttClient
{
    private $mysql_handler;
    private $mongo_handler;

    public function __construct($host,$port,$client_id,$mysql_conf,$mongo_conf)
    {
        $this->mysql_handler = new Mysqli($mysql_conf);
        $this->mongo_handler = new \MongoClient('mongodb://'.$mongo_conf['username'].':'.$mongo_conf['password'].'@'.$mongo_conf['host'].':'.$mongo_conf['port'].'/'.$mongo_conf['db']);
        parent::__construct($host,$port,$client_id);
    }

	 /**
     * override the produceContainer function and map your own class/data/closure to the injector,and they can be used in every publish receive handler
     * for exp: $client->setTopics([new Topic('test/own',function($mongo,$msg){ $result = $mongo->selectCollection('log_platform','test')->find(['sid' => ['$gte' => intval($msg)]]); })]);
     * @return Injector
     */
    protected function produceContainer()
    {
        $container = new Injector();
        $container->mapData(MqttClient::class,$this);
        $container->mapData(Client::class,$this);
        $container->mapData('mysqli',$this->mysql_handler);
        $container->mapData('mongo',$this->mongo_handler);
        return $container;
    }

}

```

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity25

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity64

Established project with proven stability

 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.

###  Release Activity

Cadence

Every ~72 days

Recently: every ~54 days

Total

10

Last Release

2549d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/8c7f383d1fbc7d01247ec7740bc2fe00036335c9227dab341b1fccdd10cfe0f3?d=identicon)[jesusslim](/maintainers/jesusslim)

---

Top Contributors

[![jesusslim](https://avatars.githubusercontent.com/u/12554966?v=4)](https://github.com/jesusslim "jesusslim (35 commits)")

---

Tags

injectmqttmqttclientphpmqttmqtt

### Embed Badge

![Health badge](/badges/jesusslim-mqttclient/health.svg)

```
[![Health](https://phpackages.com/badges/jesusslim-mqttclient/health.svg)](https://phpackages.com/packages/jesusslim-mqttclient)
```

###  Alternatives

[php-mqtt/client

An MQTT client written in and for PHP.

4372.1M30](/packages/php-mqtt-client)[php-mqtt/laravel-client

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

228539.3k1](/packages/php-mqtt-laravel-client)[simps/mqtt

MQTT Protocol Analysis and Coroutine Client for PHP

39351.6k9](/packages/simps-mqtt)[workerman/mqtt

22450.3k12](/packages/workerman-mqtt)[binsoul/net-mqtt-client-react

Asynchronous MQTT client built on React

48787.8k24](/packages/binsoul-net-mqtt-client-react)[binsoul/net-mqtt

MQTT protocol implementation

28897.8k7](/packages/binsoul-net-mqtt)

PHPackages © 2026

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