Asynchronous MQTT client for PHP based on workerman.
composer require workerman/mqtt- MQTT
- MQTT 5
- MQTT over websocket
subscribe.php
<?php
require __DIR__ . '/vendor/autoload.php';
use Workerman\Worker;
$worker = new Worker();
$worker->onWorkerStart = function(){
$mqtt = new Workerman\Mqtt\Client('mqtt://test.mosquitto.org:1883');
$mqtt->onConnect = function($mqtt) {
$mqtt->subscribe('test');
};
$mqtt->onMessage = function($topic, $content){
var_dump($topic, $content);
};
$mqtt->connect();
};
Worker::runAll();Run with command php subscribe.php start
publish.php
<?php
require __DIR__ . '/vendor/autoload.php';
use Workerman\Worker;
$worker = new Worker();
$worker->onWorkerStart = function(){
$mqtt = new Workerman\Mqtt\Client('mqtt://test.mosquitto.org:1883');
$mqtt->onConnect = function($mqtt) {
$mqtt->publish('test', 'hello workerman mqtt');
};
$mqtt->connect();
};
Worker::runAll();Run with command php publish.php start
Client::__construct()Client::connect()Client::publish()Client::subscribe()Client::unsubscribe()Client::disconnect()Client::close()callback onConnectcallback onMessagecallback onErrorcallback onClose
Create an instance by $address and $options.
-
$addresscan be on the following protocols: 'mqtt', 'mqtts', 'mqtt://test.mosquitto.org:1883'. -
$optionsis the client connection options. Defaults:keepalive:50seconds, set to0to disableclient_id: client id, defaultworkerman-mqtt-client-{$mt_rand}protocol_name:'MQTT'or 'MQIsdp'protocol_level:'MQTT'is4and 'MQIsdp' is3clean_session:true, set to false to receive QoS 1 and 2 messages while offlinereconnect_period:1second, interval between two reconnectionsconnect_timeout:30senconds, time to wait before a CONNACK is receivedusername: the username required by your broker, if anypassword: the password required by your broker, if anywill: a message that will sent by the broker automatically when the client disconnect badly. The format is:topic: the topic to publishcontent: the message to publishqos: the QoSretain: the retain flag
resubscribe: if connection is broken and reconnects, subscribed topics are automatically subscribed again (defaulttrue)bindtodefault '', used to specify the IP address that PHP will use to access the networkssldefaultfalse, it can be settrueorssl contextsee http://php.net/manual/en/context.ssl.phpdebugdefaultfalse, settrueto show debug info
Connect to broker specified by the given $address and $options in __construct($address, $options).
publish(String $topic, String $content, [array $options], [callable $callback], [array $properties])
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, default0retainretain flag,Boolean, defaultfalsedupmark as duplicate flag,Boolean, defaultfalse
$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.$properties-arrayMQTT5 user properties
Subscribe to a topic or topics
$topicis aStringtopic or anArraywhich has as keys the topic name and as value the QoS likearray('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 disconnectinggrantedis an array ofarray('topic' => 'qos', 'topic' => 'qos')where:topicis a subscribed to topicqosis the granted qos level on it
$properties-arrayMQTT5 user properties
Unsubscribe from a topic or topics
$topicis aStringtopic or an array of topics to unsubscribe from$callback-function (\Exception $e), fired on unsuback. No error occurs then$exceptionwill be null..$properties-arrayMQTT5 user properties
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, client, properties) {}
Emitted when the client receives a publish packet
$topictopic of the received packet$contentpayload of the received packet$mqttClient instance.$propertiesarrayMQTT5 user properties
Emitted when something wrong for example the client cannot connect broker.
Emitted when connection closed.
MIT