2017 © Pedro Peláez
 

library mqtt

image

workerman/mqtt

  • Monday, July 23, 2018
  • by workerman
  • Repository
  • 4 Watchers
  • 58 Stars
  • 22 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 11 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

MQTT

Asynchronous MQTT client for PHP based on workerman., (*1)

Installation

composer require workerman/mqtt

文档

中文文档, (*2)

Example

subscribe.php, (*3)

<?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, (*4)

publish.php, (*5)

<?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, (*6)

API


, (*7)

__construct (string $address, [array $options])

Create an instance by $address and $options., (*8)

  • $address can be on the following protocols: 'mqtt', 'mqtts', 'mqtt://test.mosquitto.org:1883'., (*9)

  • $options is the client connection options. Defaults:, (*10)

    • keepalive: 50 seconds, set to 0 to disable
    • client_id: client id, default workerman-mqtt-client-{$mt_rand}
    • protocol_name: 'MQTT' or 'MQIsdp'
    • protocol_level: 'MQTT' is 4 and 'MQIsdp' is 3
    • clean_session: true, set to false to receive QoS 1 and 2 messages while offline
    • reconnect_period: 1 second, interval between two reconnections
    • connect_timeout: 30 senconds, 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)
    • bindto default '', used to specify the IP address that PHP will use to access the network
    • ssl default false, it can be set true or ssl context see http://php.net/manual/en/context.ssl.php
    • debug default false, set true to show debug info

, (*11)

connect()

Connect to broker specified by the given $address and $options in __construct($address, $options)., (*12)


, (*13)

publish(String $topic, String $content, [array $options], [callable $callback], [array $properties])

Publish a message to a topic, (*14)

  • $topic is the topic to publish to, String
  • $message is the message to publish, String
  • $options is the options to publish with, including:
    • qos QoS level, Number, default 0
    • retain retain flag, Boolean, default false
    • dup mark 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 $exception will be null.
  • $properties - array MQTT5 user properties

, (*15)

subscribe(mixed $topic, [array $options], [callable $callback], [array $properties])

Subscribe to a topic or topics, (*16)

  • $topic is a String topic or an Array which has as keys the topic name and as value the QoS like array('test1'=> 0, 'test2'=> 1) to subscribe.
  • $options is the options to subscribe with, including:
    • qos qos subscription level, default 0
  • $callback - function (\Exception $exception, array $granted) callback fired on suback where:
    • exception a subscription error or an error that occurs when client is disconnecting
    • granted is an array of array('topic' => 'qos', 'topic' => 'qos') where:
    • topic is a subscribed to topic
    • qos is the granted qos level on it
  • $properties - array MQTT5 user properties

, (*17)

unsubscribe(mixed $topic, [callable $callback], [array $properties])

Unsubscribe from a topic or topics, (*18)

  • $topic is a String topic or an array of topics to unsubscribe from
  • $callback - function (\Exception $e), fired on unsuback. No error occurs then $exception will be null..
  • $properties - array MQTT5 user properties

, (*19)

disconnect()

Send DISCONNECT package to broker and close the client., (*20)


, (*21)

close()

Close the client without DISCONNECT package., (*22)


, (*23)

callback onConnect(Client $mqtt)

Emitted on successful connection (CONNACK package received)., (*24)


, (*25)

callback onMessage(String $topic, String $content, Client $mqtt, array $properties)

function (topic, message, client, properties) {}, (*26)

Emitted when the client receives a publish packet * $topic topic of the received packet * $content payload of the received packet * $mqtt Client instance. * $properties array MQTT5 user properties, (*27)


, (*28)

callback onError(\Exception $exception)

Emitted when something wrong for example the client cannot connect broker., (*29)


, (*30)

callback onClose()

Emitted when connection closed., (*31)


License

MIT, (*32)

The Versions

23/07 2018

dev-master

9999999-dev http://www.workerman.net

  Sources   Download

MIT

The Requires

 

19/07 2018