承接 workerman/mqtt 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

workerman/mqtt

Composer 安装命令:

composer require workerman/mqtt

包简介

README 文档

README

Asynchronous MQTT client for PHP based on workerman.

Installation

composer require workerman/mqtt

文档

中文文档

support

  • MQTT
  • MQTT 5
  • MQTT over websocket

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], [array $properties])

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.
  • $properties - array MQTT5 user properties

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

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
  • $properties - array MQTT5 user properties

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

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..
  • $properties - array MQTT5 user properties

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, array $properties)

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

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

callback onError(\Exception $exception)

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

callback onClose()

Emitted when connection closed.

License

MIT

workerman/mqtt 适用场景与选型建议

workerman/mqtt 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 53.91k 次下载、GitHub Stars 达 218, 最近一次更新时间为 2018 年 07 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「event-loop」 「workerman」 「mqtt」 「mqtt_client」 「mqtt5.0」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 workerman/mqtt 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 workerman/mqtt 我们能提供哪些服务?
定制开发 / 二次开发

基于 workerman/mqtt 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 53.91k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 224
  • 点击次数: 32
  • 依赖项目数: 14
  • 推荐数: 0

GitHub 信息

  • Stars: 218
  • Watchers: 12
  • Forks: 49
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-07-01