adachsoft / mqtt-contract
MQTT protocol contracts for PHP applications
Requires
- php: >=8.2
Requires (Dev)
- adachsoft/php-code-style: ^0.7.2
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.3
- phpunit/phpunit: 13.1.*
- rector/rector: ^2.8
- symplify/phpstan-rules: ^14.1
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is not auto-updated.
Last update: 2026-11-08 06:22:56 UTC
README
adachsoft/mqtt-contract provides transport-independent MQTT client contracts and message value objects for PHP applications. It defines a shared API for MQTT client integrations without binding applications to a specific broker transport.
Features
MqttClientInterfacefor connecting, publishing, subscribing, and running the MQTT event loop.- Immutable
MessageVofor representing an MQTT message. MqttQualityOfServiceLevelEnumfor the three MQTT delivery levels.MqttExceptionandMqttInvalidValueExceptionfor contract-level error handling.
This package contains contracts and value objects only. It does not include a broker connection or a concrete MQTT client implementation.
Requirements
- PHP 8.2 or later.
Installation
composer require adachsoft/mqtt-contract
Integration
Use a concrete MQTT adapter that implements MqttClientInterface, or implement the interface for your chosen transport. The adapter is responsible for broker configuration and transport-specific behavior.
The following example accepts an already configured client implementation:
use AdachSoft\Mqtt\Contract\Enum\MqttQualityOfServiceLevelEnum;
use AdachSoft\Mqtt\Contract\MqttClientInterface;
use AdachSoft\Mqtt\Contract\ValueObject\MessageVo;
function publishTemperature(MqttClientInterface $client, float $temperature): void
{
$client->connect();
try {
$client->publishMessage(
new MessageVo(
topic: 'sensors/temperature',
message: json_encode(['value' => $temperature], JSON_THROW_ON_ERROR),
qualityOfService: MqttQualityOfServiceLevelEnum::AT_LEAST_ONCE,
),
);
} finally {
$client->close();
}
}
See Basic Usage for publishing and subscription examples.