All Projects → try-to → swoole_mqtt

try-to / swoole_mqtt

Licence: MIT license
一个基于swoole的异步mqtt 客户端库,可用于接收或者发送mqtt协议的消息。支持QoS 0、QoS 1、QoS 2。支持MQTT 3.1和3.1.1版本。 参考workerman-mqtt

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to swoole mqtt

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 (+75.61%)
Mutual labels:  swoole, mqtt-client
swoft-ddz
基于swoft框架开发斗地主
Stars: ✭ 70 (+70.73%)
Mutual labels:  swoole
mqtt-go
Yet another Go MQTT 3.1.1 client library: Go-ish interface, extensible and thread-safe (experimental stage; the API may change in future release)
Stars: ✭ 29 (-29.27%)
Mutual labels:  mqtt-client
MqttAndroidExample
An example Android app using MQTT protocol
Stars: ✭ 19 (-53.66%)
Mutual labels:  mqtt-client
homebrew-extensions
🍻 Homebrew tap for PHP extensions
Stars: ✭ 264 (+543.9%)
Mutual labels:  swoole
request-callback
➰ Swoole request callback for PSR compliant handlers.
Stars: ✭ 22 (-46.34%)
Mutual labels:  swoole
kafka-swoole
☝️Implement all kafka protocols, providing 'HighLevel' and 'LowLevel' client apis respectively, and utilize swoole to realize collaboration and flexibly extend consumers' client, which is the first php-kafka client to support multiple compression forms
Stars: ✭ 33 (-19.51%)
Mutual labels:  swoole
sdebug
Xdebug — Step Debugger and Debugging Aid for PHP
Stars: ✭ 263 (+541.46%)
Mutual labels:  swoole
one-app
one 框架初项目始化
Stars: ✭ 19 (-53.66%)
Mutual labels:  swoole
zhamao-framework
协程、高性能、灵活的聊天机器人 & Web 开发框架(炸毛框架)
Stars: ✭ 99 (+141.46%)
Mutual labels:  swoole
ultraman
奥特曼
Stars: ✭ 45 (+9.76%)
Mutual labels:  swoole
swoole-ide-helper
💪 IDE helper for Swoft and Swoole
Stars: ✭ 42 (+2.44%)
Mutual labels:  swoole
chubbyphp-swoole-request-handler
A request handler adapter for swoole, using PSR-7, PSR-15 and PSR-17.
Stars: ✭ 22 (-46.34%)
Mutual labels:  swoole
mqtt-panel
Self hosted Web App panel for MQTT
Stars: ✭ 29 (-29.27%)
Mutual labels:  mqtt-client
Jarolift MQTT
Controlling Jarolift TDEF 433MHZ radio shutters via ESP8266 and CC1101 Transceiver Module in asynchronous mode
Stars: ✭ 43 (+4.88%)
Mutual labels:  mqtt-client
skeleton
基于Swoole的轻量、高效、全异步开源框架。
Stars: ✭ 71 (+73.17%)
Mutual labels:  swoole
vertica-swoole-adapter
Provides a DB layer for Swoole-based applications to communicate to HP Vertica databases.
Stars: ✭ 14 (-65.85%)
Mutual labels:  swoole
can2mqtt
Bidirectional CAN-Bus to MQTT-Bridge
Stars: ✭ 39 (-4.88%)
Mutual labels:  mqtt-client
yafcms
基于yaf+swoole的高效率,快如闪电的博客cms系统!
Stars: ✭ 17 (-58.54%)
Mutual labels:  swoole
thrift2-hbase
thrift2-hbase component for Hyperf.
Stars: ✭ 14 (-65.85%)
Mutual labels:  swoole

MQTT

Asynchronous MQTT client for PHP based on swoole.

Installation

composer require try-to/swoole_mqtt

文档

中文文档

Example

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

API

  • Client::__construct()
  • Client::connect()
  • Client::reconnect()
  • Client::publish()
  • Client::subscribe()
  • Client::unsubscribe()
  • Client::disconnect()
  • Client::close()
  • callback onConnect
  • callback onMessage
  • callback onError
  • callback onClose

__construct (string $host, int $port, [array $options])

  • $host Service address.

  • $port port.

  • $options is the client connection options. Defaults:

    • keepalive: 50 seconds, set to 0 to disable
    • client_id: client id, default swoole-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 service __construct($host, $port, $options).


reconnect()

Reconnect service __construct($host, $port, $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.


material

https://github.com/walkor/mqtt

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