elemenx/laravel-mqtt
Composer 安装命令:
composer require elemenx/laravel-mqtt
包简介
A simple Laravel Library to connect/publish/subscribe to MQTT broker
关键字:
README 文档
README
A simple Laravel Library to connect/publish/subscribe to MQTT broker
Based on bluerhinos/phpMQTT
For Example see this repo
Installation
composer require elemenx/laravel-mqtt
Features
- Name and password authentication
- Client certificate authentication
- Certificate Protection for end to end encryption
- Enable Debug mode to make it easier for debugging
- Now you can also set Client_id of your choice and if you don't want just simply don't use or set it to null
- Set QOS flag directly from config file
- Set Retain flag directly from config file
- Addition of Helper functions to make development more easy
Enable the package (Optional)
This package implements Laravel auto-discovery feature. After you install it the package provider and facade are added automatically for laravel >= 5.5.
This step is only required if you are using laravel version <5.5
To declare the provider and/or alias explicitly, then add the service provider to your config/app.php:
'providers' => [
Elemenx\Mqtt\MqttServiceProvider::class,
];
And then add the alias to your config/app.php:
'aliases' => [
'Mqtt' => \Elemenx\Mqtt\Facades\Mqtt::class,
];
Configuration
Publish the configuration file
php artisan vendor:publish --provider="Elemenx\Mqtt\MqttServiceProvider"
Config/mqtt.php
'host' => env('mqtt_host','127.0.0.1'),
'password' => env('mqtt_password',''),
'username' => env('mqtt_username',''),
'certfile' => env('mqtt_cert_file',''),
'localcert' => env('mqtt_local_cert', ''),
'localpk' => env('mqtt_local_pk', ''),
'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
Publishing topic
use Elemenx\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";
}
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 Elemenx\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);
}
Subscribing topic using Facade
use Mqtt;
public function subscribetoTopic($topic)
{
//You can also subscribe to multiple topics using the same function $topic can be array of topics e.g ['topic1', 'topic2']
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);
}
Happy Coding...!
elemenx/laravel-mqtt 适用场景与选型建议
elemenx/laravel-mqtt 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.17k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 08 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「laravel」 「PHP 5.4」 「laravel5」 「laravel-5」 「php 7.0」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 elemenx/laravel-mqtt 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 elemenx/laravel-mqtt 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 elemenx/laravel-mqtt 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Common elements of AloFramework
Laravel Integers Hashids Generator. Encode and decode ids to Integers. Also generate prefixed hashids
Basic CAS (SSO) authenticator for Symfony 5.4, 6.0, 7.0 and 8.0
A simple Laravel Library to connect/publish/subscribe to MQTT broker
Laravel Json Locations Manager. Import all app language strings from lang folder
Laravel Paginator for Collections or Arrays
统计信息
- 总下载量: 1.17k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-08-09