anviz/php-mqtt-client

1.0.0 2020-07-10 09:33 UTC

This package is auto-updated.

Last update: 2024-09-10 19:26:40 UTC


README

Latest Stable Version Total Downloads License

php-mqtt/client was created by, and is maintained by Jacobs. It allows you to connect to an MQTT broker where you can publish messages and subscribe to topics. The implementation supports all QoS levels (with limitations

Installation

composer require anviz/mqttclient

This library requires PHP version 7.2 or higher.

Usage

Publish

A very basic publish example requires only three steps: connect, publish and close

$clientId = 'test-publisher';

$mqtt = new MQTTClient($server, $port, $clientId);
$mqtt->connect();
$mqtt->publish('php-mqtt/client/test', 'Hello World!', 0);
$mqtt->close();

If you do not want to pass a $clientId, a random one will be generated for you. This will basically force a clean session implicitly.

Be also aware that most of the methods can throw exceptions. The above example does not add any exception handling for brevity.

Subscribe

Subscribing is a little more complex than publishing as it requires to run an event loop:

$clientId = 'test-subscriber';

$mqtt = new MQTTClient($server, $port, $clientId);
$mqtt->connect();
$mqtt->subscribe('php-mqtt/client/test', function ($topic, $message) {
    echo sprintf("Received message on topic [%s]: %s\n", $topic, $message);
}, 0);
$mqtt->loop(true);

While the loop is active, you can use $mqtt->interrupt() to send an interrupt signal to the loop. This will terminate the loop before it starts its next iteration. You can call this method using pcntl_signal(SIGINT, $handler) for example:

pcntl_async_signals(true);

$clientId = 'test-subscriber';

$mqtt = new MQTTClient($server, $port, $clientId);
pcntl_signal(SIGINT, function (int $signal, $info) use ($mqtt) {
    $mqtt->interrupt();
});
$mqtt->connect();
$mqtt->subscribe('php-mqtt/client/test', function ($topic, $message) {
    echo sprintf("Received message on topic [%s]: %s\n", $topic, $message);
}, 0);
$mqtt->loop(true);
$mqtt->close();

Features

  • MQTT Versions
    • v3.1
    • v3.1.1
    • v5.0
  • Transport
    • TCP (unsecured)
    • TLS (secured, verifies the peer using a certificate authority file)
  • Connect
    • Last Will
    • Last Will Topic
    • Last Will Message
    • Required QoS
    • Message Retention
    • Authentication (username & password)
    • Clean Session (can be set and sent, but the client has no persistence for QoS 2 messages)
  • Publish
    • QoS Level 0
    • QoS Level 1 (limitation: no persisted state across sessions)
    • QoS Level 2 (limitation: no persisted state across sessions)
  • Subscribe
    • QoS Level 0
    • QoS Level 1
    • QoS Level 2 (limitation: no persisted state across sessions)
  • Supported Message Length: unlimited (no limits enforced, although the MQTT protocol supports only up to 256MB which one shouldn't use even remotely anyway)
  • Logging possible (Psr\Log\LoggerInterface can be passed to the client)
  • Persistence Drivers
    • In-Memory Driver
    • Redis Driver

Limitations

  • There is no guarantee that message identifiers are not used twice (while the first usage is still pending). The current implementation uses a simple counter which resets after all 65535 identifiers were used. This means that as long as the client isn't used to an extent where acknowledgements are open for a very long time, you should be fine. This also only affects QoS levels higher than 0, as QoS level 0 is a simple fire and forget mode.
  • Message flows with a QoS level higher than 0 are not persisted as the default implementation uses an in-memory repository for data. To avoid issues with broken message flows, use the clean session flag to indicate that you don't care about old data.

License

anviz/php-mqtt-client is open-sourced software licensed under the MIT license.