Asynchronous MQTT client for PHP based on swoole.
composer require try-to/swoole_mqtt
subscribe.php
<?php
use TrytoMqtt\Client;
require_once __DIR__ . '/vendor/autoload.php';
$options = [
    'clean_session' => false,
    'client_id' => 'demo-subscribe-123456',
    'username' => '',
    'password' => '',
];
$mqtt = new Client('127.0.0.1', 1883, $options);
$mqtt->onConnect = function ($mqtt) {
    $mqtt->subscribe('/World');
};
$mqtt->onMessage = function ($topic, $content) {
    var_dump($topic, $content);
};
$mqtt->onError = function ($exception) use ($mqtt) {
    echo "error\n";
    // $mqtt->reconnect(1000);
};
$mqtt->onClose = function () {
    echo "close\n";
};
$mqtt->connect();Run with command php subscribe.php
publish.php
<?php
use TrytoMqtt\Client;
require_once __DIR__ . '/../vendor/autoload.php';
$options = [
    'clean_session' => false,
    'client_id' => 'demo-publish-123456',
    'username' => '',
    'password' => '',
];
$mqtt = new Client('127.0.0.1', 1883, $options);
$mqtt->onConnect = function ($mqtt) {
    $mqtt->publish('/World', 'hello swoole mqtt');
};
$mqtt->onError = function ($exception) {
    echo "error\n";
};
$mqtt->onClose = function () {
    echo "close\n";
};
$mqtt->connect();Run with command php publish.php
- Client::__construct()
- Client::connect()
- Client::reconnect()
- Client::publish()
- Client::subscribe()
- Client::unsubscribe()
- Client::disconnect()
- Client::close()
- callback onConnect
- callback onMessage
- callback onError
- callback onClose
- 
$hostService address.
- 
$portport.
- 
$optionsis the client connection options. Defaults:- keepalive:- 50seconds, set to- 0to disable
- client_id: client id, default- swoole-mqtt-client-{$mt_rand}
- protocol_name:- 'MQTT'or '- MQIsdp'
- protocol_level:- 'MQTT'is- 4and '- MQIsdp' is- 3
- clean_session:- true, set to false to receive QoS 1 and 2 messages while offline
- reconnect_period:- 1second, interval between two reconnections
- connect_timeout:- 30senconds, time to wait before a CONNACK is received
- username: the username required by your broker, if any
- password: the password required by your broker, if any
- will: a message that will sent by the broker automatically when the client disconnect badly. The format is:- topic: the topic to publish
- content: the message to publish
- qos: the QoS
- retain: the retain flag
 
- resubscribe: if connection is broken and reconnects, subscribed topics are automatically subscribed again (default- true)
- bindtodefault '', used to specify the IP address that PHP will use to access the network
- ssldefault- false, it can be set- trueor- ssl contextsee http://php.net/manual/en/context.ssl.php
- debugdefault- false, set- trueto show debug info
 
Connect service  __construct($host, $port, $options).
Reconnect service  __construct($host, $port, $options).
Publish a message to a topic
- $topicis the topic to publish to,- String
- $messageis the message to publish,- String
- $optionsis the options to publish with, including:- qosQoS level,- Number, default- 0
- retainretain flag,- Boolean, default- false
- dupmark as duplicate flag,- Boolean, default- false
 
- $callback-- function (\Exception $exception), fired when the QoS handling completes, or at the next tick if QoS 0. No error occurs then- $exceptionwill be null.
Subscribe to a topic or topics
- $topicis a- Stringtopic or an- Arraywhich has as keys the topic name and as value the QoS like- array('test1'=> 0, 'test2'=> 1)to subscribe.
- $optionsis the options to subscribe with, including:- qosqos subscription level, default 0
 
- $callback-- function (\Exception $exception, array $granted)callback fired on suback where:- exceptiona subscription error or an error that occurs when client is disconnecting
- grantedis an array of- array('topic' => 'qos', 'topic' => 'qos')where:- topicis a subscribed to topic
- qosis the granted qos level on it
 
 
Unsubscribe from a topic or topics
- $topicis a- Stringtopic or an array of topics to unsubscribe from
- $callback-- function (\Exception $e), fired on unsuback. No error occurs then- $exceptionwill be null..
Send DISCONNECT package to broker and close the client.
Close the client without DISCONNECT package.
Emitted on successful connection (CONNACK package received).
function (topic, message, packet) {}
Emitted when the client receives a publish packet
- $topictopic of the received packet
- $contentpayload of the received packet
- $mqttClient instance.
Emitted when something wrong for example the client cannot connect broker.
Emitted when connection closed.