alexmorbo/react-mqtt

Async MQTT client in reactphp

0.2 2022-02-23 17:32 UTC

This package is auto-updated.

Last update: 2024-04-23 22:07:51 UTC


README

react-mqtt is an MQTT client library for PHP.

Its based on the reactPHP socket-client and added the MQTT protocol specific functions. Also based on https://github.com/oliverlorenz/phpMqttClient

Goal

Goal of this project is easy to use MQTT client for PHP in a modern architecture without using any php modules. Currently, only protocol version 4 (mqtt 3.1.1) is implemented.

Example library initial

// mqtt.php

use Morbo\React\Mqtt\Client;
use Morbo\React\Mqtt\ConnectionOptions;
use Morbo\React\Mqtt\Protocols\Version4;

require_once __DIR__ . '/vendor/autoload.php';

// Creating Event Loop
$loop = React\EventLoop\Factory::create();

// Connection configuration
$config = [
    'host' => 'localhost',
    'port' => 1883,
//    'options' => new ConnectionOptions([
//        'username' => 'auth_user',
//        'password' => 'auth_password',
//        'clientId' => 'react_client', // default is 'react-'.uniqid()
//        'cleanSession' => true, // default is true
//        'cleanSession' => true, // default is true
// .      'willTopic' => '',
// .      'willMessage' => '',
// .      'willQos' => '',
// .      'willRetain' => '',
// .      'keepAlive' => 60, // default is 60
//    ])
];

$mqtt = new Client($loop, new Version4());

Example publish

use React\Socket\ConnectionInterface;

require 'mqtt.php';

$connection = $mqtt->connect($config['host'], $config['port'], $config['options']);

$connection->then(function (ConnectionInterface $stream) use ($mqtt, $loop) {
    /**
     * Stop loop, when client disconnected from mqtt server
     */
    $stream->on('end', function () use ($loop) {
        $loop->stop();
    });

    $data = [
        'foo' => 'bar',
        'bar' => 'baz',
        'time' => time(),
    ];

    $qos = Morbo\React\Mqtt\Packets\QoS\Levels::AT_MOST_ONCE_DELIVERY;  // 0

    $mqtt->publish($stream, 'foo/bar', json_encode($data), $qos)->then(function (ConnectionInterface $stream) use ($mqtt) {
        /**
         * Disconnect when published
         */
        $mqtt->disconnect($stream);
    });
});


$loop->run();

Example subscribe

use Morbo\React\Mqtt\Packets;
use React\Socket\ConnectionInterface;

require 'mqtt.php';

$connection = $mqtt->connect($config['host'], $config['port'], $config['options']);

$connection->then(function (ConnectionInterface $stream) use ($mqtt) {
    $qos = Morbo\React\Mqtt\Packets\QoS\Levels::AT_MOST_ONCE_DELIVERY;  // 0
    $mqtt->subscribe($stream, 'foo/bar', $qos)->then(function (ConnectionInterface $stream) use ($qos) {
        // Success subscription
        $stream->on(Packets\Publish::EVENT, function(Packets\Publish $publish) {
            var_dump($publish);
        });
    }, function ($error) {
        // Subscription error
    });
});

$loop->run();

Avaiable methods

Currently works:

  • connect (clean session, will options, keepalive, connection authorization)
  • disconnect
  • publish
  • subscribe