PHPackages                             shunhua/laravel-mqtt - 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. [HTTP &amp; Networking](/categories/http)
4. /
5. shunhua/laravel-mqtt

ActiveLibrary[HTTP &amp; Networking](/categories/http)

shunhua/laravel-mqtt
====================

A simple Laravel Library to connect/publish/subscribe to MQTT broker

v1.0(5y ago)11782[1 issues](https://github.com/shunhua/mqtt/issues)MITPHPPHP &gt;=7.3

Since Jan 26Pushed 4y ago1 watchersCompare

[ Source](https://github.com/shunhua/mqtt)[ Packagist](https://packagist.org/packages/shunhua/laravel-mqtt)[ RSS](/packages/shunhua-laravel-mqtt/feed)WikiDiscussions master Synced today

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

Laravel MQTT 扩展包
================

[](#laravel-mqtt-扩展包)

A simple Laravel Library to connect/publish/subscribe to MQTT broker

支持实例化多应用配置

安装
--

[](#安装)

```
composer require shunhua/laravel-mqtt

```

启用 (可选)
-------

[](#启用-可选)

This package implements Laravel auto-discovery feature. After you install it the package provider and facade are added automatically for laravel &gt;= 5.5.

**This step is only required if you are using laravel version &lt;5.5**

To declare the provider and/or alias explicitly, then add the service provider to your config/app.php:

```
'providers' => [

        Salman\Mqtt\MqttServiceProvider::class,
];

```

And then add the alias to your config/app.php:

```
'aliases' => [

       'Mqtt' => \Salman\Mqtt\Facades\Mqtt::class,
];

```

配置
--

[](#配置)

Publish the configuration file

```
php artisan vendor:publish --provider="Salman\Mqtt\MqttServiceProvider"

```

Config/mqtt.php
---------------

[](#configmqttphp)

```
    'host'     => env('mqtt_host','127.0.0.1'),
    'password' => env('mqtt_password',''),
    'username' => env('mqtt_username',''),
    'certfile' => env('mqtt_cert_file',''),
    'port'     => env('mqtt_port','1883'),
    'debug'    => env('mqtt_debug',false) //Optional Parameter to enable debugging set it to True
    'qos'      => env('mqtt_qos', 0), // set quality of service here
    'retain'   => env('mqtt_retain', 0) // it should be 0 or 1 Whether the message should be retained.- Retain Flag

```

#### 发布

[](#发布)

```
use Salman\Mqtt\MqttClass\Mqtt;

public function SendMsgViaMqtt($topic, $message)
{
        $mqtt = new Mqtt();
        $client_id = Auth::user()->id;
        $output = $mqtt->ConnectAndPublish($topic, $message, $client_id);

        if ($output === true)
        {
            return "published";
        }

           return "Failed";
}

```

#### 使用 Facade 发布

[](#使用-facade-发布)

```
use Mqtt;

public function SendMsgViaMqtt($topic, $message)
{
        $client_id = Auth::user()->id;

        $output = Mqtt::ConnectAndPublish($topic, $message, $client_id);

        if ($output === true)
        {
            return "published";
        }

        return "Failed";
}

```

#### 订阅

[](#订阅)

```
use Salman\Mqtt\MqttClass\Mqtt;

public function SubscribetoTopic($topic)
    {
        $mqtt = new Mqtt();
        $client_id = Auth::user()->id;
        $mqtt->ConnectAndSubscribe($topic, function($topic, $msg){
            echo "Msg Received: \n";
            echo "Topic: {$topic}\n\n";
            echo "\t$msg\n\n";
        }, $client_id);

    }

```

#### 使用 Facade 订阅

[](#使用-facade-订阅)

```
use Mqtt;

public function SubscribetoTopic($topic)
    {
       Mqtt::ConnectAndSubscribe($topic, function($topic, $msg){
            echo "Msg Received: \n";
            echo "Topic: {$topic}\n\n";
            echo "\t$msg\n\n";
        },$client_id);

    }

```

#### 使用 Helper 发布

[](#使用-helper-发布)

```

public function SendMsgViaMqtt($topic, $message)
{
        $client_id = Auth::user()->id;

        $output = connectToPublish($topic, $message, $client_id);

        if ($output === true)
        {
            return "published";
        }

        return "Failed";
}

```

#### 使用 Helper 订阅

[](#使用-helper-订阅)

```
public function SubscribetoTopic($topic)
{
   return connectToSubscribe($topic,$client_id);
}

```

### Tested on php 7.3 and laravel 5.7 and also laravel 5.8 and also on laravel 6

[](#tested-on-php-73-and-laravel-57-and-also-laravel-58-and-also-on-laravel-6)

#### Also supports php 7.4

[](#also-supports-php-74)

###  Health Score

26

—

LowBetter than 41% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 66.7% 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

Unknown

Total

1

Last Release

1984d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/11988030?v=4)[bryant](/maintainers/shunhua)[@shunhua](https://github.com/shunhua)

---

Top Contributors

[![liubaoqiang-max](https://avatars.githubusercontent.com/u/237364336?v=4)](https://github.com/liubaoqiang-max "liubaoqiang-max (4 commits)")[![shunhua](https://avatars.githubusercontent.com/u/11988030?v=4)](https://github.com/shunhua "shunhua (2 commits)")

---

Tags

laravelmqttphplaravellaravel5php-7.4PHP 5.4php 7.0php 7.1php 7.2php 7.3mqtt-laravelmqtt-laravel-publishermqtt-laravel-subscriberlaravel mqtt librarylaravel-mqtt

### Embed Badge

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

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

###  Alternatives

[salmanzafar/laravel-mqtt

A simple Laravel Library to connect/publish/subscribe to MQTT broker

102157.1k1](/packages/salmanzafar-laravel-mqtt)[salmanzafar/laravel-geocode

A Laravel Library to find Lat and Long of a given Specific Address

154.4k](/packages/salmanzafar-laravel-geocode)[basement-chat/basement-chat

Add a real-time chat widget to your Laravel application.

4984.0k](/packages/basement-chat-basement-chat)

PHPackages © 2026

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