mattia/mqtt-bundle
Composer 安装命令:
composer require mattia/mqtt-bundle
包简介
A Symfony bundle for seamless MQTT integration and IoT messaging
README 文档
README
A comprehensive Symfony bundle for MQTT (Message Queuing Telemetry Transport) messaging with full TLS support and easy integration.
✨ Features
- 🚀 Seamless Integration: Simple Symfony service injection and configuration
- 🔒 Enterprise TLS Support: Complete TLS/SSL configuration with certificate management
- 📦 JSON Message Handling: Built-in JSON serialization and deserialization
- 🎯 Full QoS Support: All MQTT Quality of Service levels (0, 1, 2)
- 🔧 Flexible Configuration: Environment-based configuration
- 📊 PSR-3 Logging: Comprehensive logging integration for debugging and monitoring
- 🛠️ Console Commands: Built-in CLI tools for testing and diagnostics
🚀 Coming Soon
We are actively working on expanding the bundle's capabilities to provide a seamless integration between Symfony and MQTT.
Below is our planned roadmap — stay tuned for updates!
- Event Dispatcher integration for incoming messages
- Multi-broker configuration support
- More Console commands
- MQTT v5 features (user properties, reason codes, etc.)
- Symfony Flex recipe for quick installation & configuration
- Examples & Docker setup for local development
📦 Installation
Step 1: Install via Composer
composer require mattia/mqtt-bundle
Step 2: Register the Bundle
Add the bundle to your config/bundles.php:
<?php return [ // ... other bundles Mattia\MqttBundle\MattiaMqttBundle::class => ['all' => true], ];
Step 3: Configure Environment Variables
Add the following to your .env file:
# MQTT Configuration MQTT_HOST=localhost MQTT_PORT=1883 MQTT_CLIENT_ID=symfony_mqtt_client MQTT_USERNAME= MQTT_PASSWORD= MQTT_USE_TLS=false MQTT_CONNECT_TIMEOUT=10 # TLS Configuration (only used when MQTT_USE_TLS=true) MQTT_TLS_VERIFY_PEER=true MQTT_TLS_VERIFY_PEER_NAME=true MQTT_TLS_SELF_SIGNED_ALLOWED=false MQTT_TLS_CA_FILE= MQTT_TLS_CA_PATH= MQTT_TLS_CLIENT_CERT_FILE= MQTT_TLS_CLIENT_CERT_KEY_FILE= MQTT_TLS_CLIENT_CERT_KEY_PASSPHRASE=
Step 4: Configure the Bundle
Create config/packages/mattia_mqtt.yaml:
mattia_mqtt: mqtt: host: "%env(MQTT_HOST)%" port: "%env(int:MQTT_PORT)%" client_id: "%env(MQTT_CLIENT_ID)%" username: "%env(MQTT_USERNAME)%" password: "%env(MQTT_PASSWORD)%" use_tls: "%env(bool:MQTT_USE_TLS)%" tls_verify_peer: "%env(bool:MQTT_TLS_VERIFY_PEER)%" tls_verify_peer_name: "%env(bool:MQTT_TLS_VERIFY_PEER_NAME)%" tls_self_signed_allowed: "%env(bool:MQTT_TLS_SELF_SIGNED_ALLOWED)%" tls_ca_file: "%env(resolve:MQTT_TLS_CA_FILE)%" tls_ca_path: "%env(resolve:MQTT_TLS_CA_PATH)%" tls_client_cert_file: "%env(resolve:MQTT_TLS_CLIENT_CERT_FILE)%" tls_client_cert_key_file: "%env(resolve:MQTT_TLS_CLIENT_CERT_KEY_FILE)%" tls_client_cert_key_passphrase: "%env(MQTT_TLS_CLIENT_CERT_KEY_PASSPHRASE)%" connect_timeout: "%env(int:MQTT_CONNECT_TIMEOUT)%"
🚀 Usage
Basic Integration
Inject the MQTT service into your controllers or services:
<?php namespace App\Controller; use Mattia\MqttBundle\Service\MqttService; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; class SensorController extends AbstractController { public function __construct( private MqttService $mqttService ) {} public function publishSensorData(): void { // Publish a simple message $this->mqttService->publish('sensors/temperature', '25.5°C', 0, false); // Publish structured JSON data $this->mqttService->publishJson('sensors/data', [ 'temperature' => 25.5, 'humidity' => 60, 'timestamp' => date('c'), 'location' => 'room-101' ]); } public function subscribeToSensorData(): void { $this->mqttService->subscribe('sensors/+', 0, function (string $topic, string $message, bool $retained) { // Handle received message $this->logger->info('Received sensor data', [ 'topic' => $topic, 'message' => $message, 'retained' => $retained ]); }); } }
Service API Reference
The MqttService provides a comprehensive API for MQTT operations:
📤 Publishing Messages
// Publish a simple string message $mqttService->publish(string $topic, string $message, int $qos = 0, bool $retain = false): void // Publish structured JSON data $mqttService->publishJson(string $topic, array $data, int $qos = 0, bool $retain = false): void
📥 Subscribing to Topics
// Subscribe with callback function $mqttService->subscribe(string $topic, int $qos = 0, callable $callback = null): void // Subscribe with automatic JSON decoding $mqttService->subscribeJson(string $topic, int $qos = 0, callable $callback = null): void // Unsubscribe from a topic $mqttService->unsubscribe(string $topic): void
🔌 Connection Management
// Check connection status $mqttService->isConnected(): bool // Disconnect from broker $mqttService->disconnect(): void
🔒 TLS Configuration
// Get current TLS configuration for debugging $mqttService->getTlsConfiguration(): array
🛠️ Console Commands
The bundle includes a built-in console command for testing connectivity:
# Basic connectivity test php bin/console mqtt:ping # Detailed test with configuration display php bin/console mqtt:ping --details
📊 Debugging and Logging
Enable debug logging by configuring your logger to show MQTT-related messages:
# config/packages/monolog.yaml monolog: channels: ["mqtt"] handlers: main: type: stream path: "%kernel.logs_dir%/%kernel.environment%.log" level: debug channels: ["mqtt"] console: type: console level: debug channels: ["mqtt"]
📋 Requirements
- PHP: 8.1 or higher
- Symfony: 6.4 or higher
- MQTT Broker: Mosquitto, Eclipse Mosquitto, HiveMQ, or any MQTT broker
📄 License
This bundle is licensed under the MIT License. See the LICENSE file for details.
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
mattia/mqtt-bundle 适用场景与选型建议
mattia/mqtt-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 7, 最近一次更新时间为 2025 年 09 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「bundle」 「iot」 「mqtt」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mattia/mqtt-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mattia/mqtt-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mattia/mqtt-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
2lenet/EasyAdminPlusBundle
The bundle for easy using json-rpc api on your project
Provide a way to secure accesses to all routes of an symfony application.
DMS Meetup API Bundle, enables Meetup API clients in services
Client library for connecting to SignalWire.
Bundle Symfony DaplosBundle
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-09-08