Search by

adachsoft / mqtt-contract

Arkadiusz Adach

MQTT protocol contracts for PHP applications

Package info

gitlab.com/a.adach/mqtt-contract

Issues

pkg:composer/adachsoft/mqtt-contract

Statistics

Installs: 13

Dependents: 1

Suggesters: 0

Stars: 0

v0.1.0 2026-11-07 08:38 UTC

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

  • MqttClientInterface for connecting, publishing, subscribing, and running the MQTT event loop.
  • Immutable MessageVo for representing an MQTT message.
  • MqttQualityOfServiceLevelEnum for the three MQTT delivery levels.
  • MqttException and MqttInvalidValueException for 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.