承接 timeshow/laravel-mqtt 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

timeshow/laravel-mqtt

最新稳定版本:v0.2

Composer 安装命令:

composer require timeshow/laravel-mqtt

包简介

Mqtt Connect/Publish/Subscribe for Laravel

README 文档

README

It is a mqtt plugin package wrapper for the Laravel and allows you to connect to an MQTT broker where you can publish messages and subscribe to topics.

Version Compatibility

Laravel Package
9.0 dev
10.0^ last version

Install

$ composer require timeshow/laravel-mqtt

The package will register itself through Laravel auto discovery of packages. Registered will be the service provider as well as an Mqtt facade. Add the MqttServiceProvider to your config/app.php:

//providers
'providers' => [
    // ...
    TimeShow\Mqtt\MqttServiceProvider::class,
]
    
//aliases
'aliases' => [
    //...
    'Mqtt' => TimeShow\Mqtt\Facades\Mqtt::class,    
]

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

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

Configure(.env)

MQTT_HOST=your_emqx_server_address
MQTT_USERNAME=your_username
MQTT_PASSWORD=your_password
MQTT_PORT=1883

Publishing topic

use Timeshow\Mqtt\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";
}

Publishing topic using 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";
}

Subscribing topic

use Timeshow\Mqtt\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);


    }

Subscribing topic using Facade

You can also subscribe to multiple topics using the same function $topic can be array of topics e.g ['topic1', 'topic2']

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);


    }

Publishing topic using Helper method

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

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

        return "Failed";
}

Subscribing topic using Helper method

You can also subscribe to multiple topics using the same function $topic can be array of topics e.g ['topic1', 'topic2']

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

统计信息

  • 总下载量: 5
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-10