php-mqtt/laravel-client 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

php-mqtt/laravel-client

Composer 安装命令:

composer require php-mqtt/laravel-client

包简介

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

README 文档

README

Latest Stable Version Total Downloads Tests Quality Gate Status Maintainability Rating Reliability Rating Security Rating Vulnerabilities License

php-mqtt/laravel-client was created by, and is maintained by Marvin Mall. It is a Laravel wrapper for the php-mqtt/client package and allows you to connect to an MQTT broker where you can publish messages and subscribe to topics.

Installation

The package is available on packagist.org and can be installed using composer:

composer require php-mqtt/laravel-client

The package will register itself through Laravel auto discovery of packages. Registered will be the service provider as well as an MQTT facade.

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

php artisan vendor:publish --provider="PhpMqtt\Client\MqttClientServiceProvider" --tag="config"

and change the configuration in config/mqtt-client.php according to your needs.

Configuration

The package allows you to configure multiple named connections. An initial example with inline documentation can be found in the published configuration file. Most of the configuration options are optional and come with sane defaults (especially all of the connection_settings).

An example configuration of two connections, where one is meant for sharing public data and one for private data, could look like the following:

'default_connection' => 'private',

'connections' => [
    'private' => [
        'host' => 'mqtt.example.com',
        'port' => 1883,
    ],
    'public' => [
        'host' => 'test.mosquitto.org',
        'port' => 1883,
    ],
],

In this example, the private connection is the default one.

Note: it is recommended to use environment variables to configure the MQTT client. Available environment variables can be found in the configuration file.

Usage

Publish (QoS level 0)

Publishing a message with QoS level 0 is quite easy and can be done in a single command:

use PhpMqtt\Client\Facades\MQTT;

MQTT::publish('some/topic', 'Hello World!');

If needed, the retain flag (default: false) can be passed as third and the connection name as fourth parameter:

use PhpMqtt\Client\Facades\MQTT;

MQTT::publish('some/topic', 'Hello World!', true, 'public');

Using MQTT::publish($topic, $message) will implicitly call MQTT::connection(), but the connection will not be closed after usage. If you want to close the connection manually because your script does not need the connection anymore, you can call MQTT:disconnect() (optionally with the connection name as a parameter). Please also note that using MQTT::publish($topic, $message) will always use QoS level 0. If you need a different QoS level, you will need to use the MqttClient directly which is explained below.

Publish (QoS level 1 & 2)

Different to QoS level 0, we need to run an event loop in order for QoS 1 and 2 to work. This is because with a one-off command we cannot guarantee that a message reaches its target. The event loop will ensure a published message gets sent again if no acknowledgment is returned by the broker within a grace period. Only when the broker returns an acknowledgement (or all of the acknowledgements in case of QoS 2), the client will stop resending the message.

use PhpMqtt\Client\Facades\MQTT;

/** @var \PhpMqtt\Client\Contracts\MqttClient $mqtt */
$mqtt = MQTT::connection();
$mqtt->publish('some/topic', 'foo', 1);
$mqtt->publish('some/other/topic', 'bar', 2, true); // Retain the message
$mqtt->loop(true);

$mqtt->loop() actually starts an infinite loop. To escape it, there are multiple options. In case of simply publishing a message, all we want is to receive an acknowledgement. Therefore, we can simply pass true as second parameter to exit the loop as soon as all resend queues are cleared:

/** @var \PhpMqtt\Client\Contracts\MqttClient $mqtt */
$mqtt->loop(true, true);

In order to escape the loop, you can also call $mqtt->interrupt() which will exit the loop during the next iteration. The method can, for example, be called in a registered signal handler:

/** @var \PhpMqtt\Client\Contracts\MqttClient $mqtt */
pcntl_signal(SIGINT, function () use ($mqtt) {
    $mqtt->interrupt();
});

Subscribe

Very similar to publishing with QoS level 1 and 2, subscribing requires to run an event loop. Although before running the loop, topics need to be subscribed to:

use PhpMqtt\Client\Facades\MQTT;

/** @var \PhpMqtt\Client\Contracts\MqttClient $mqtt */
$mqtt = MQTT::connection();
$mqtt->subscribe('some/topic', function (string $topic, string $message) {
    echo sprintf('Received QoS level 1 message on topic [%s]: %s', $topic, $message);
}, 1);
$mqtt->loop(true);

Features

This library allows you to use all the features provided by php-mqtt/client. Simply retrieve an instance of \PhpMqtt\Client\Contracts\MqttClient with MQTT::connection(string $name = null) and use it directly.

For an extensive collection of examples which explain how to use the MQTT client (directly), you can visit the php-mqtt/client-examples repository.

License

php-mqtt/laravel-client is open-source software licensed under the MIT license.

php-mqtt/laravel-client 适用场景与选型建议

php-mqtt/laravel-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 623.71k 次下载、GitHub Stars 达 226, 最近一次更新时间为 2021 年 01 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「client」 「publish」 「laravel」 「subscribe」 「mqtt」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 php-mqtt/laravel-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 php-mqtt/laravel-client 我们能提供哪些服务?
定制开发 / 二次开发

基于 php-mqtt/laravel-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 623.71k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 233
  • 点击次数: 23
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 226
  • Watchers: 5
  • Forks: 22
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-01-05