{"id":36332909,"url":"https://github.com/try-to/swoole_mqtt","last_synced_at":"2026-01-11T12:01:36.508Z","repository":{"id":57073115,"uuid":"161730151","full_name":"try-to/swoole_mqtt","owner":"try-to","description":"一个基于swoole的异步mqtt 客户端库，可用于接收或者发送mqtt协议的消息。支持QoS 0、QoS 1、QoS 2。支持MQTT 3.1和3.1.1版本。 参考workerman-mqtt","archived":false,"fork":false,"pushed_at":"2020-05-09T06:22:51.000Z","size":46,"stargazers_count":43,"open_issues_count":8,"forks_count":9,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-19T11:23:26.322Z","etag":null,"topics":["mqtt","mqtt-client","php","swoole"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/try-to.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-12-14T04:08:56.000Z","updated_at":"2023-12-27T16:44:02.000Z","dependencies_parsed_at":"2022-08-24T14:54:39.895Z","dependency_job_id":null,"html_url":"https://github.com/try-to/swoole_mqtt","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/try-to/swoole_mqtt","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/try-to%2Fswoole_mqtt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/try-to%2Fswoole_mqtt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/try-to%2Fswoole_mqtt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/try-to%2Fswoole_mqtt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/try-to","download_url":"https://codeload.github.com/try-to/swoole_mqtt/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/try-to%2Fswoole_mqtt/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28302005,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-11T11:18:18.743Z","status":"ssl_error","status_checked_at":"2026-01-11T11:07:56.842Z","response_time":60,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mqtt","mqtt-client","php","swoole"],"created_at":"2026-01-11T12:01:29.215Z","updated_at":"2026-01-11T12:01:36.469Z","avatar_url":"https://github.com/try-to.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MQTT\r\nAsynchronous MQTT client for PHP based on swoole.\r\n\r\n# Installation\r\ncomposer require try-to/swoole_mqtt\r\n\r\n# 文档\r\n[中文文档](https://github.com/try-to/swoole_mqtt/blob/master/README_CN.md)\r\n\r\n# Example\r\n**subscribe.php**\r\n```php\r\n\u003c?php\r\nuse TrytoMqtt\\Client;\r\nrequire_once __DIR__ . '/vendor/autoload.php';\r\n$options = [\r\n    'clean_session' =\u003e false,\r\n    'client_id' =\u003e 'demo-subscribe-123456',\r\n    'username' =\u003e '',\r\n    'password' =\u003e '',\r\n];\r\n$mqtt = new Client('127.0.0.1', 1883, $options);\r\n$mqtt-\u003eonConnect = function ($mqtt) {\r\n    $mqtt-\u003esubscribe('/World');\r\n};\r\n$mqtt-\u003eonMessage = function ($topic, $content) {\r\n    var_dump($topic, $content);\r\n};\r\n$mqtt-\u003eonError = function ($exception) use ($mqtt) {\r\n    echo \"error\\n\";\r\n    // $mqtt-\u003ereconnect(1000);\r\n};\r\n$mqtt-\u003eonClose = function () {\r\n    echo \"close\\n\";\r\n};\r\n$mqtt-\u003econnect();\r\n```\r\nRun with command ```php subscribe.php```\r\n\r\n**publish.php**\r\n```php\r\n\u003c?php\r\nuse TrytoMqtt\\Client;\r\nrequire_once __DIR__ . '/../vendor/autoload.php';\r\n$options = [\r\n    'clean_session' =\u003e false,\r\n    'client_id' =\u003e 'demo-publish-123456',\r\n    'username' =\u003e '',\r\n    'password' =\u003e '',\r\n];\r\n$mqtt = new Client('127.0.0.1', 1883, $options);\r\n$mqtt-\u003eonConnect = function ($mqtt) {\r\n    $mqtt-\u003epublish('/World', 'hello swoole mqtt');\r\n};\r\n$mqtt-\u003eonError = function ($exception) {\r\n    echo \"error\\n\";\r\n};\r\n$mqtt-\u003eonClose = function () {\r\n    echo \"close\\n\";\r\n};\r\n$mqtt-\u003econnect();\r\n```\r\n\r\nRun with command ```php publish.php```\r\n\r\n## API\r\n\r\n  * Client::__construct()\r\n  * Client::connect()\r\n  * Client::reconnect()\r\n  * Client::publish()\r\n  * Client::subscribe()\r\n  * Client::unsubscribe()\r\n  * Client::disconnect()\r\n  * Client::close()\r\n  * callback onConnect\r\n  * callback onMessage\r\n  * callback onError\r\n  * callback onClose\r\n\r\n-------------------------------------------------------\r\n\r\n### __construct (string $host, int $port, [array $options])\r\n\r\n  * `$host` Service address. \r\n  * `$port` port.\r\n\r\n  * `$options` is the client connection options. Defaults:\r\n    * `keepalive`: `50` seconds, set to `0` to disable\r\n    * `client_id`: client id, default `swoole-mqtt-client-{$mt_rand}`\r\n    * `protocol_name`: `'MQTT'` or '`MQIsdp`'\r\n    * `protocol_level`: `'MQTT'` is `4` and '`MQIsdp`' is `3`\r\n    * `clean_session`: `true`, set to false to receive QoS 1 and 2 messages while\r\n      offline\r\n    * `reconnect_period`: `1` second, interval between two reconnections\r\n    * `connect_timeout`: `30` senconds, time to wait before a CONNACK is received\r\n    * `username`: the username required by your broker, if any\r\n    * `password`: the password required by your broker, if any\r\n    * `will`: a message that will sent by the broker automatically when\r\n       the client disconnect badly. The format is:\r\n      * `topic`: the topic to publish\r\n      * `content`: the message to publish\r\n      * `qos`: the QoS\r\n      * `retain`: the retain flag\r\n    * `resubscribe` : if connection is broken and reconnects,\r\n       subscribed topics are automatically subscribed again (default `true`)\r\n    * `bindto` default '', used to specify the IP address that PHP will use to access the network\r\n    * `ssl` default `false`, it can be set `true` or `ssl context` see http://php.net/manual/en/context.ssl.php\r\n    * `debug` default `false`, set `true` to show debug info\r\n\r\n-------------------------------------------------------\r\n\r\n### connect()\r\n\r\nConnect service  `__construct($host, $port, $options)`.\r\n\r\n-------------------------------------------------------\r\n\r\n### reconnect()\r\n\r\nReconnect service  `__construct($host, $port, $options)`.\r\n\r\n-------------------------------------------------------\r\n\r\n### publish(String $topic, String $content, [array $options], [callable $callback])\r\n\r\nPublish a message to a topic\r\n\r\n* `$topic` is the topic to publish to, `String`\r\n* `$message` is the message to publish, `String`\r\n* `$options` is the options to publish with, including:\r\n  * `qos` QoS level, `Number`, default `0`\r\n  * `retain` retain flag, `Boolean`, default `false`\r\n  * `dup` mark as duplicate flag, `Boolean`, default `false`\r\n* `$callback` - `function (\\Exception $exception)`, fired when the QoS handling completes,\r\n  or at the next tick if QoS 0. No error occurs then `$exception` will be null.\r\n  \r\n-------------------------------------------------------\r\n\r\n### subscribe(mixed $topic, [array $options], [callable $callback])\r\n\r\nSubscribe to a topic or topics\r\n\r\n* `$topic` is a `String` topic or an `Array` which has as keys the topic name and as value\r\nthe QoS like `array('test1'=\u003e 0, 'test2'=\u003e 1)` to subscribe.\r\n* `$options` is the options to subscribe with, including:\r\n  * `qos` qos subscription level, default 0\r\n* `$callback` - `function (\\Exception $exception, array $granted)`\r\n  callback fired on suback where:\r\n  * `exception` a subscription error or an error that occurs when client is disconnecting\r\n  * `granted` is an array of `array('topic' =\u003e 'qos', 'topic' =\u003e 'qos')` where:\r\n    * `topic` is a subscribed to topic\r\n    * `qos` is the granted qos level on it\r\n\r\n-------------------------------------------------------\r\n\r\n### unsubscribe(mixed $topic, [callable $callback])\r\n\r\nUnsubscribe from a topic or topics\r\n\r\n* `$topic` is a `String` topic or an array of topics to unsubscribe from\r\n* `$callback` - `function (\\Exception $e)`, fired on unsuback. No error occurs then `$exception` will be null..\r\n\r\n-------------------------------------------------------\r\n\r\n### disconnect()\r\n\r\nSend DISCONNECT package to broker and close the client.\r\n\r\n-------------------------------------------------------\r\n\r\n### close()\r\n\r\nClose the client without DISCONNECT package.\r\n\r\n-------------------------------------------------------\r\n\r\n### callback onConnect(Client $mqtt)\r\nEmitted on successful connection (`CONNACK` package received).\r\n\r\n-------------------------------------------------------\r\n\r\n### callback onMessage(String $topic, String $content, Client $mqtt)\r\n`function (topic, message, packet) {}`\r\n\r\nEmitted when the client receives a publish packet\r\n* `$topic` topic of the received packet\r\n* `$content` payload of the received packet\r\n* `$mqtt` Client instance.\r\n\r\n-------------------------------------------------------\r\n\r\n### callback onError(\\Exception $exception)\r\nEmitted when something wrong for example the client cannot connect broker.\r\n\r\n-------------------------------------------------------\r\n\r\n### callback onClose()\r\nEmitted when connection closed.\r\n\r\n-------------------------------------------------------\r\n\r\n### material\r\nhttps://github.com/walkor/mqtt\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftry-to%2Fswoole_mqtt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftry-to%2Fswoole_mqtt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftry-to%2Fswoole_mqtt/lists"}