vs-point/messenger-transport-mqtt
Composer 安装命令:
composer require vs-point/messenger-transport-mqtt
包简介
Symfony Messenger transport for the MQTT.
README 文档
README
Symfony Messenger transport for MQTT, built on top of php-mqtt/client.
Requirements
- PHP >= 8.2
- Symfony Messenger ^7.0 | ^8.0
- A running MQTT broker (e.g. Eclipse Mosquitto)
Installation
composer require vs-point/messenger-transport-mqtt
How it works
Sending: any message class implementing MqttMessageInterface dispatched to the bus is published to the MQTT broker on the topic and with the QoS level returned by the message. Messages with QoS > 0 wait up to 300 ms for the broker acknowledgment before returning.
Receiving: the Messenger worker calls get() in a loop. Each call opens a 100 ms non-blocking poll against the broker and returns all messages that arrived during that window as MqttMessage envelopes. Subscribed topics are configured on the transport factory and established on the first get() call. All subscriptions use QoS 0.
ack() and reject() are no-ops — delivery guarantees are handled at the MQTT protocol level via QoS.
Configuration
1. Register the transport factory
# config/services.yaml VSPoint\Messenger\Transport\Mqtt\MqttTransportFactory: arguments: $topics: ['/sensors/#', '/commands/+'] $clientId: '%env(MQTT_CLIENT_ID)%' tags: ['messenger.transport_factory']
$topics — list of MQTT topic filters the worker subscribes to. Wildcards (#, +) are supported.
$clientId — optional; defaults to the current process ID.
2. Configure the transport
# config/packages/messenger.yaml framework: messenger: transports: mqtt: '%env(MESSENGER_MQTT_TRANSPORT_DSN)%' routing: App\Message\SensorReading: mqtt
3. Environment variables
# .env
MESSENGER_MQTT_TRANSPORT_DSN=mqtt://user:pass@broker.example.com:1883
MQTT_CLIENT_ID=my-app-worker
Omit credentials for brokers that allow anonymous connections:
MESSENGER_MQTT_TRANSPORT_DSN=mqtt://broker.example.com:1883
Usage
Sending a message
Implement MqttMessageInterface in your message class to control the topic, QoS level, and payload:
<?php namespace App\Message; use VSPoint\Messenger\Transport\Mqtt\MqttMessageInterface; final class SensorReading implements MqttMessageInterface { public function __construct( private readonly string $sensorId, private readonly float $value, ) { } public function getTopic(): string { return '/sensors/' . $this->sensorId; } public function getQos(): int { return 1; // at least once } public function getBody(): string { return (string) $this->value; } }
Dispatch it via the message bus:
$bus->dispatch(new SensorReading('temp-01', 23.4));
Receiving messages
Start the Messenger worker:
php bin/console messenger:consume mqtt
The worker receives MqttMessage envelopes. Handle them with a message handler:
<?php namespace App\MessageHandler; use Symfony\Component\Messenger\Attribute\AsMessageHandler; use VSPoint\Messenger\Transport\Mqtt\MqttMessage; #[AsMessageHandler] final class MqttMessageHandler { public function __invoke(MqttMessage $message): void { // $message->getTopic() — MQTT topic the message arrived on // $message->getBody() — raw payload string // $message->getQos() — QoS level (always 0 for received messages) // $message->getId() — unique ID generated on receipt } }
Note: received messages are always wrapped in
MqttMessage, regardless of the original message class used for sending. The transport does not perform any serialization —getBody()carries the raw string payload.
Development
Start the local environment
docker compose build docker compose up -d mqtt docker compose run --rm php composer install
Run tests
Tests require the MQTT broker to be running (docker compose up -d mqtt):
docker compose run --rm php ./vendor/bin/phpunit --testdox
Static analysis
docker compose run --rm php ./vendor/bin/phpstan analyse --memory-limit=512M
Coding standard
# check docker compose run --rm php ./vendor/bin/ecs check # fix docker compose run --rm php ./vendor/bin/ecs check --fix
vs-point/messenger-transport-mqtt 适用场景与选型建议
vs-point/messenger-transport-mqtt 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 130 次下载、GitHub Stars 达 8, 最近一次更新时间为 2019 年 05 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「component」 「Messenger」 「mqtt」 「symfony-messenger」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 vs-point/messenger-transport-mqtt 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 vs-point/messenger-transport-mqtt 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 vs-point/messenger-transport-mqtt 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
FSi DataSource Component
The bundle for easy using json-rpc api on your project
Enables the creation of cron-jobs tasks to be consumed by symfony/messenger
Messenger functionality for Laravel 5.2.
统计信息
- 总下载量: 130
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 8
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-05-01