8000 GitHub - AndreTeixeira1998/client: An MQTT client written in and for PHP.
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

AndreTeixeira1998/client

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

59 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

php-mqtt/client

Latest Stable Version Total Downloads Tests License

php-mqtt/client was created by, and is maintained by Namoshek. 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 php-mqtt/client

This library requires PHP version 7.2 or higher.

Usage

Publish

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

$server   = 'some-broker.example.com';
$port     = 1883;
$clientId = 'test-publisher';

$mqtt = new \PhpMqtt\Client\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 \PhpMqtt\Client\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 \PhpMqtt\Client\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();

Client Settings

As shown in the examples above, the MQTTClient takes the server, port and client id as first, second and third parameter. As fourth parameter, the path to a CA file can be passed which will enable TLS and is used to verify the peer. A fifth parameter allows passing a repository (currently, only a MemoryRepository is available by default). Lastly, a logger can be passed as sixth parameter. If none is given, a null logger is used instead.

Example:

$mqtt = new \PhpMqtt\Client\MQTTClient(
    $server, 
    $port, 
    $clientId,
    '/path/to/ca/file',
    new \PhpMqtt\Client\Repositories\MemoryRepository(),
    new Logger()
);

The logger must implement the Psr\Log\LoggerInterface.

Connection Settings

The connect() method of the MQTTClient takes four optional parameters:

  1. Username
  2. Password
  3. A ConnectionSettings instance
  4. A boolean flag indicating whether a clean session should be requested (a random client id does this implicitly)

Example:

$mqtt = new \PhpMqtt\Client\MQTTClient($server, $port, $clientId);

$connectionSettings = new \PhpMqtt\Client\ConnectionSettings();
$mqtt->connect($username, $password, $connectionSettings, true);

The ConnectionSettings class has the following constructor and defaults:

public function __construct(
    int $qualityOfService = 0,
    bool $retain = false,
    bool $blockSocket = false,
    int $socketTimeout = 5,
    int $keepAlive = 10,
    int $resendTimeout = 10,
    string $lastWillTopic = null,
    string $lastWillMessage = null,
    bool $useTls = false,
    bool $tlsVerifyPeer = true,
    bool $tlsVerifyName = true,
    string $tlsClientCertificateFile = null,
    string $tlsClientCertificateKeyFile = null,
    string $tlsClientCertificatePassphrase = null
) { ... }

Features

  • MQTT Versions
    • v3 (just don't use v3.1 features like username & password)
    • 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

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

About

An MQTT client written in and for PHP.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%
0