All Projects → walkor → Mqtt

walkor / Mqtt

Asynchronous MQTT client for PHP based on workerman.

Projects that are alternatives of or similar to Mqtt

Nyamuk
Python MQTT Client Library Based on libmosquitto
Stars: ✭ 72 (-49.3%)
Mutual labels:  mqtt, mqtt-client
Client
An MQTT client written in and for PHP.
Stars: ✭ 90 (-36.62%)
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 (-49.3%)
Mutual labels:  mqtt, mqtt-client
Lua Mosquitto
Lua bindings to the libmosquitto MQTT client library.
Stars: ✭ 47 (-66.9%)
Mutual labels:  mqtt, mqtt-client
Tuya Mqtt
Nodejs-Script to combine tuyaapi and openhab via mqtt
Stars: ✭ 105 (-26.06%)
Mutual labels:  mqtt, mqtt-client
Luamqtt
luamqtt - Pure-lua MQTT v3.1.1 and v5.0 client
Stars: ✭ 58 (-59.15%)
Mutual labels:  mqtt, mqtt-client
Coogleiot
A ESP8266 Library for easy IOT device development
Stars: ✭ 118 (-16.9%)
Mutual labels:  mqtt, mqtt-client
Hbmqtt
MQTT client/broker using Python asynchronous I/O
Stars: ✭ 667 (+369.72%)
Mutual labels:  mqtt, mqtt-client
Mqttandroidclient
Android消息推送MQTT
Stars: ✭ 131 (-7.75%)
Mutual labels:  mqtt, mqtt-client
Ntex Mqtt
MQTT Client/Server framework for v5 and v3.1.1 protocols
Stars: ✭ 95 (-33.1%)
Mutual labels:  mqtt, mqtt-client
Phpmqttclient
a mqtt client library for php
Stars: ✭ 33 (-76.76%)
Mutual labels:  mqtt, mqtt-client
Paho.mqtt.java
Eclipse Paho Java MQTT client library. Paho is an Eclipse IoT project.
Stars: ✭ 1,620 (+1040.85%)
Mutual labels:  mqtt, mqtt-client
Paho.mqttdotnet
A .Net wrapper for eclipse/paho.mqtt.c
Stars: ✭ 33 (-76.76%)
Mutual labels:  mqtt, mqtt-client
Mqtt Explorer
An all-round MQTT client that provides a structured topic overview
Stars: ✭ 1,162 (+718.31%)
Mutual labels:  mqtt, mqtt-client
Mqttx
MQTT X - Elegant MQTT 5.0 Client Tool of Cross-platform
Stars: ✭ 892 (+528.17%)
Mutual labels:  mqtt, mqtt-client
Mqtt
MQTT Client class
Stars: ✭ 86 (-39.44%)
Mutual labels:  mqtt, mqtt-client
Adafruit mqtt library
Arduino library for MQTT support
Stars: ✭ 441 (+210.56%)
Mutual labels:  mqtt, mqtt-client
Mosquitto Php
A wrapper for the Eclipse Mosquitto™ MQTT client library for PHP.
Stars: ✭ 448 (+215.49%)
Mutual labels:  mqtt, mqtt-client
Psmqtt
Utility reporting system health and status via MQTT
Stars: ✭ 95 (-33.1%)
Mutual labels:  mqtt, mqtt-client
Applozic Ios Sdk
iOS Real Time Chat & Messaging SDK
Stars: ✭ 104 (-26.76%)
Mutual labels:  mqtt, mqtt-client

MQTT

Asynchronous MQTT client for PHP based on workerman.

Installation

composer require workerman/mqtt

文档

中文文档

Example

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

API


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

Create an instance by $address and $options.

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

  • $options is the client connection options. Defaults:

    • 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

connect()

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


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

Publish a message to a topic

  • $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.

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

Subscribe to a topic or topics

  • $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

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

Unsubscribe from a topic or topics

  • $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..

disconnect()

Send DISCONNECT package to broker and close the client.


close()

Close the client without DISCONNECT package.


callback onConnect(Client $mqtt)

Emitted on successful connection (CONNACK package received).


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

function (topic, message, packet) {}

Emitted when the client receives a publish packet

  • $topic topic of the received packet
  • $content payload of the received packet
  • $mqtt Client instance.

callback onError(\Exception $exception)

Emitted when something wrong for example the client cannot connect broker.


callback onClose()

Emitted when connection closed.


License

MIT

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].