try-to/swoole_mqtt
Composer 安装命令:
composer require try-to/swoole_mqtt
包简介
swoole mqtt client
README 文档
README
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])
-
$hostService address. -
$portport. -
$optionsis the client connection options. Defaults:keepalive:50seconds, set to0to disableclient_id: client id, defaultswoole-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()
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
$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.
subscribe(mixed $topic, [array $options], [callable $callback])
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
unsubscribe(mixed $topic, [callable $callback])
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..
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
$topictopic of the received packet$contentpayload of the received packet$mqttClient instance.
callback onError(\Exception $exception)
Emitted when something wrong for example the client cannot connect broker.
callback onClose()
Emitted when connection closed.
material
try-to/swoole_mqtt 适用场景与选型建议
try-to/swoole_mqtt 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.04k 次下载、GitHub Stars 达 43, 最近一次更新时间为 2018 年 12 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 try-to/swoole_mqtt 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 try-to/swoole_mqtt 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.04k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 43
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-12-14