All Projects → php-mqtt → Client

php-mqtt / Client

Licence: mit
An MQTT client written in and for PHP.

Projects that are alternatives of or similar to Client

Adafruit mqtt library
Arduino library for MQTT support
Stars: ✭ 441 (+390%)
Mutual labels:  mqtt, mqtt-client
Paho.mqttdotnet
A .Net wrapper for eclipse/paho.mqtt.c
Stars: ✭ 33 (-63.33%)
Mutual labels:  mqtt, mqtt-client
Mosquitto Php
A wrapper for the Eclipse Mosquitto™ MQTT client library for PHP.
Stars: ✭ 448 (+397.78%)
Mutual labels:  mqtt, mqtt-client
Tdm
GUI application to discover and monitor devices flashed with https://github.com/arendst/Sonoff-Tasmota
Stars: ✭ 385 (+327.78%)
Mutual labels:  mqtt, mqtt-client
Mqtt
🕹 MQTT Protocol Analysis and Coroutine Client for PHP. Support for 3.1, 3.1.1 and 5.0 versions of the MQTT protocol.
Stars: ✭ 72 (-20%)
Mutual labels:  mqtt, mqtt-client
Hivemq Mqtt Client
HiveMQ MQTT Client is an MQTT 5.0 and MQTT 3.1.1 compatible and feature-rich high-performance Java client library with different API flavours and backpressure support
Stars: ✭ 402 (+346.67%)
Mutual labels:  mqtt, mqtt-client
Mqttx
MQTT X - Elegant MQTT 5.0 Client Tool of Cross-platform
Stars: ✭ 892 (+891.11%)
Mutual labels:  mqtt, mqtt-client
Kmansonoff
Firmware for ESP8266 based itead Sonoff switches for use with HomeAssistant / mqtt
Stars: ✭ 282 (+213.33%)
Mutual labels:  mqtt, mqtt-client
Luamqtt
luamqtt - Pure-lua MQTT v3.1.1 and v5.0 client
Stars: ✭ 58 (-35.56%)
Mutual labels:  mqtt, mqtt-client
Lua Mosquitto
Lua bindings to the libmosquitto MQTT client library.
Stars: ✭ 47 (-47.78%)
Mutual labels:  mqtt, mqtt-client
Mqtt C
A portable MQTT C client for embedded systems and PCs alike.
Stars: ✭ 342 (+280%)
Mutual labels:  mqtt, mqtt-client
Mqtt
MQTT Client class
Stars: ✭ 86 (-4.44%)
Mutual labels:  mqtt, mqtt-client
Wolfmqtt
wolfMQTT is a small, fast, portable MQTT client implementation, including support for TLS 1.3.
Stars: ✭ 316 (+251.11%)
Mutual labels:  mqtt, mqtt-client
Qmqtt
MQTT Client for Qt
Stars: ✭ 409 (+354.44%)
Mutual labels:  mqtt, mqtt-client
Mqtt Panel
A web interface for MQTT
Stars: ✭ 315 (+250%)
Mutual labels:  mqtt, mqtt-client
Hbmqtt
MQTT client/broker using Python asynchronous I/O
Stars: ✭ 667 (+641.11%)
Mutual labels:  mqtt, mqtt-client
asyncio-mqtt
Idomatic asyncio wrapper around paho-mqtt
Stars: ✭ 137 (+52.22%)
Mutual labels:  mqtt, mqtt-client
node-deepstackai-trigger
Detects motion using Deepstack AI and calls registered triggers based on trigger rules.
Stars: ✭ 154 (+71.11%)
Mutual labels:  mqtt, mqtt-client
Phpmqttclient
a mqtt client library for php
Stars: ✭ 33 (-63.33%)
Mutual labels:  mqtt, mqtt-client
Mqtt Explorer
An all-round MQTT client that provides a structured topic overview
Stars: ✭ 1,162 (+1191.11%)
Mutual labels:  mqtt, mqtt-client

php-mqtt/client

Latest Stable Version Total Downloads Coverage Quality Gate Status Maintainability Rating Reliability Rating Security Rating Vulnerabilities License

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

Installation

The package is available on packagist.org and can be installed using composer:

composer require php-mqtt/client

The package requires PHP version 7.4 or higher.

Usage

In the following, only a few very basic examples are given. For more elaborate examples, have a look at the php-mqtt/client-examples repository.

Publish

A very basic publish example using QoS 0 requires only three steps: connect, publish and disconnect

$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->disconnect();

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 which reads, parses and handles messages from the broker:

$server   = 'some-broker.example.com';
$port     = 1883;
$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);
$mqtt->disconnect();

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->disconnect();

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 protocol level can be passed. Currently supported is MQTT v3.1, available as constant MqttClient::MQTT_3_1. 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,
    \PhpMqtt\Client\MqttClient::MQTT_3_1,
    new \PhpMqtt\Client\Repositories\MemoryRepository(),
    new Logger()
);

The Logger must implement the Psr\Log\LoggerInterface.

Connection Settings

The connect() method of the MqttClient takes two optional parameters:

  1. A ConnectionSettings instance
  2. 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)
    ->setConnectTimeout(3)
    ->setUseTls(true)
    ->setTlsSelfSignedAllowed(true);
    
$mqtt->connect($connectionSettings, true);

The ConnectionSettings class provides a few settings through a fluent interface. The type itself is immutable, and a new ConnectionSettings instance will be created for each added option. This also prevents changes to the connection settings after a connection has been established.

The following is a complete list of options with their respective default:

$connectionSettings = (new \PhpMqtt\Client\ConnectionSettings)
    ->setUsername(null)
    ->setPassword(null)
    ->setConnectTimeout(60)
    ->setSocketTimeout(5)
    ->setKeepAliveInterval(10)
    ->setResendTimeout(10)
    ->setLastWillTopic(null)
    ->setLastWillMessage(null)
    ->setLastWillQualityOfService(0)
    ->setRetainLastWill(false)
    ->setUseTls(false)
    ->setTlsVerifyPeer(true)
    ->setTlsVerifyPeerName(true)
    ->setTlsSelfSignedAllowed(false)
    ->setTlsCertificateAuthorityFile(null)
    ->setTlsCertificateAuthorityPath(null)
    ->setTlsClientCertificateFile(null)
    ->setTlsClientCertificateKeyFile(null)
    ->setTlsClientCertificateKeyPassphrase(null);

Features

  • Supported MQTT Versions
    • [x] v3 (just don't use v3.1 features like username & password)
    • [x] v3.1
    • [ ] v3.1.1
    • [ ] v5.0
  • Transport
    • [x] TCP (unsecured)
    • [x] TLS (secured, verifies the peer using a certificate authority file)
  • Connect
    • [x] Last Will
    • [x] Message Retention
    • [x] Authentication (username & password)
    • [x] TLS encrypted connections
    • [ ] Clean Session (can be set and sent, but the client has no persistence for QoS 2 messages)
  • Publish
    • [x] QoS Level 0
    • [x] QoS Level 1 (limitation: no persisted state across sessions)
    • [x] QoS Level 2 (limitation: no persisted state across sessions)
  • Subscribe
    • [x] QoS Level 0
    • [x] QoS Level 1
    • [x] 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
    • [x] 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. It will not only instruct the broker to consider the connection new (without previous state), but will also reset the registered repository.

Developing & Testing

Certificates (TLS)

To run the tests (especially the TLS tests), you will need to create certificates. A command has been provided for this:

sh create-certificates.sh

This will create all required certificates in the .ci/tls/ directory. The same script is used for continuous integration as well.

MQTT Broker for Testing

Running the tests expects an MQTT broker to be running. The easiest way to run an MQTT broker is through Docker:

docker run --rm -it -p 1883:1883 -p 8883:8883 -p 8884:8884 -v $(pwd)/.ci/tls:/mosquitto-certs -v $(pwd)/.ci/mosquitto.conf:/mosquitto/config/mosquitto.conf eclipse-mosquitto:1.6

When run from the project directory, this will spawn a Mosquitto MQTT broker configured with the generated TLS certificates and a custom configuration.

In case you intend to run a different broker or using a different method, or use a public broker instead, you will need to adjust the environment variables defined in phpunit.xml accordingly.

License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].