Phpmqtt V1.3.1 release, mqtt protocol analysis & coroutine client

Time:2021-6-11

In the last version of Zhihu’s article, there was a user comment. When the handshake failed, they contacted me and tested it. I really have no problem here, but I deleted a piece of code:

$will = [
    'topic' => '',
    'qos' => 0,
    'retain' => 0,
    'message' => '',
];
$client->connect(true, $will);

Change to directconnect

$client->connect();

Because mqtt3 is used, there is no code attribute in mqtt5. You need to capture packets to get the cause of the error.

Here, after changing to mqtt5, you can directly get the cause of the error. For example, the following code can get the cause of the error

use Simps\MQTT\Client;
use Simps\MQTT\Config\ClientConfig;
use Simps\MQTT\Hex\ReasonCode;
use function Swoole\Coroutine\run;

run(function () {
    $config = new ClientConfig();
    $config->setUserName('')
        ->setPassword('')
        ->setClientId(Client::genClientID())
        ->setKeepAlive(10)
        ->setProtocolLevel(5)
        ->setDelay(3000) // 3s
        ->setMaxAttempts(5)
        ->setSwooleConfig([
            'open_mqtt_protocol' => true,
            'package_max_length' => 2 * 1024 * 1024,
        ]);

    $client = new Client('broker.emqx.io', 1883, $config);
    $will = [
        'topic' => '',
        'qos' => 0,
        'retain' => 0,
        'message' => 'byebye',
    ];
    $res = $client->connect(true, $will);
    var_dump($res, ReasonCode::getReasonPhrase($res['code']));
});

Replace the code with the reason to get the error reason as follows:Topic Name invalid

array(3) {
  ["type"]=>
  int(2)
  ["session_present"]=>
  int(0)
  ["code"]=>
  int(144)
}
string(18) "Topic Name invalid"

If you view the mqtt protocol document, you will find that:

The Will Topic MUST be a UTF-8 Encoded String as defined

Topic in the will message cannot be empty. It must be a valid UTF-8 string with a length greater than 0

Therefore, a restriction is added in this version. If there is a will message and the topic is empty, an exception will be thrown directly

PHP Fatal error:  Uncaught Simps\MQTT\Exception\ProtocolException: Topic cannot be empty

Update log

enhance

  • Add ismqtt5 method (f27f85c)
  • Increase constant visibility (# 41)
  • Add the getarray parameter to the getcontents method for the client to reply to the peer ack (# 42)
  • Add message test (8f7fe30)
  • Update message document (a41654b)
  • Update protocolexception and add test (19a6bee)

repair

  • Topic of repair will message cannot be empty (b56fda1)

About phpmqtt

  • Mqtt protocol analysis & coroutine client
  • Mqtt protocol parsing and coroutine client for PHP
  • Support mqtt protocol version 3.1, 3.1.1 and 5.0, support QoS 0, QoS 1 and QoS 2
  • The first PHP library supporting mqtt V5.0 protocol

file:https://mqtt.simps.io
GitHub:https://github.com/simps/mqtt
Gitee:https://gitee.com/phpiot/mqtt

Support, remember to order a star~

Phpmqtt V1.3.1 release, mqtt protocol analysis & coroutine client