{"id":21244493,"url":"https://github.com/jesusslim/mqttclient","last_synced_at":"2025-09-23T14:19:10.053Z","repository":{"id":56998302,"uuid":"99201177","full_name":"jesusslim/mqttclient","owner":"jesusslim","description":"php mqtt client","archived":false,"fork":false,"pushed_at":"2019-05-21T10:02:19.000Z","size":48,"stargazers_count":20,"open_issues_count":1,"forks_count":17,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-09-07T22:21:27.578Z","etag":null,"topics":["inject","mqtt","mqttclient","phpmqtt"],"latest_commit_sha":null,"homepage":null,"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/jesusslim.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":"2017-08-03T06:57:40.000Z","updated_at":"2021-01-24T03:13:06.000Z","dependencies_parsed_at":"2022-08-21T11:10:20.449Z","dependency_job_id":null,"html_url":"https://github.com/jesusslim/mqttclient","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jesusslim%2Fmqttclient","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jesusslim%2Fmqttclient/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jesusslim%2Fmqttclient/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jesusslim%2Fmqttclient/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jesusslim","download_url":"https://codeload.github.com/jesusslim/mqttclient/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225184640,"owners_count":17434370,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["inject","mqtt","mqttclient","phpmqtt"],"created_at":"2024-11-21T01:26:41.647Z","updated_at":"2025-09-23T14:19:05.001Z","avatar_url":"https://github.com/jesusslim.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# jesusslim/mqttclient\n\nPHP mqtt client\n\n## usage\n\n[English]\n[Chinese](README_CN.md)\n\n### Install\n\nInstall:\n\n\tcomposer require jesusslim/mqttclient\n\nIf your composer not allowed dev-master,add this config\n\n\t\"minimum-stability\": \"dev\"\n\t\ninto your composer.json.\n\n### Require\n\n\tswoole 2.0.8+(swoole.so)\n    mosquitto.so\n\n### Example\n\nThere's MqttClient we can use.One is based on swoole(see swoole example,deprecated),another is based on mosquitto(see mosquitto example).\n\nThe difference:\n* Requirements difference.\n* Swoole has package-separation bug and crash bug.We should handle this exception,for example,monitor the process id and restart when is crash or receive error package.\n* Swoole client can subscribe and publish in one client,use mosquitto we need two client.\n* Some grammar difference between swoole and mosquitto.\n\n#### Example base on mosquitoo\n\ndefine your logger:\n\n    class Logger implements \\mqttclient\\src\\swoole\\MqttLogInterface {\n\n\t\tpublic function log($type,$content,$params = []){\n\t\t        echo \"$type : $content \\r\\n\";\n\t\t }\n\t}\n\nuse Mqttclient\n\n    $host = '127.0.0.1';\n\t$port = 1883;\n    $r = new \\mqttclient\\src\\mosquitto\\MqttClient($host,$port,10017);\n    $r-\u003esetAuth('username','password');\n    $r-\u003esetKeepAlive(60);\n    $r-\u003esetLogger(new Logger());\n    $r-\u003esetMaxReconnectTimesWhenError(360*12);\n    //reconnect interval\n    $r-\u003esetReconnectInterval(10);\n    //subscribe topics,callback's params can be any data we mapped into the container(IOC)\n    $r-\u003esetTopics(\n    [\n        new \\mqttclient\\src\\subscribe\\Topic('test/slim',function($msg){\n            echo \"I receive:\".$msg.\"\\r\\n\";}),\n        new \\mqttclient\\src\\subscribe\\Topic('test/slim3',function(\\mqttclient\\src\\swoole\\MqttClient $client,$msg){\n            echo \"I receive:\".$msg.\" for slim3 \\r\\n\";\n            echo $client-\u003egetClientId();\n        })\n    ]\n    );\n    //set trigger\n    $r-\u003eon(\\mqttclient\\src\\consts\\ClientTriggers::SOCKET_CONNECT,function(){\n        //do something\n    });\n    $r-\u003estart();\n    \nSender:\n\n    $host = '127.0.0.1';\n    $port = 1883;\n    $r = new \\mqttclient\\src\\mosquitto\\MqttSender($host,$port,10017);\n    $r-\u003esetAuth('username','password');\n    $r-\u003esetKeepAlive(60);\n    $r-\u003esetLogger(new Logger());\n    $r-\u003esetMaxReconnectTimesWhenError(360*12);\n    //reconnect interval\n    $r-\u003esetReconnectInterval(10);\n    $r-\u003esetQueue(new Queue());\n    $r-\u003estart();\n\nIt need a queue implements mqttclient\\src\\mosquitto\\MqttSendingQueue,to get msg that need to be sent in loop.\n\n#### Example base on swoole(deprecated)\n\ndefine your logger:\n\n    class Logger implements \\mqttclient\\src\\swoole\\MqttLogInterface {\n\n\t\tpublic function log($type,$content,$params = []){\n\t\t        echo \"$type : $content \\r\\n\";\n\t\t }\n\t}\n\ndefine your tmp store (use Redis/Memory/...)\n\n\tclass Store implements \\mqttclient\\src\\swoole\\TmpStorageInterface{\n\n    \tprivate $data = [];\n\n\t    public function set($message_type, $key, $sub_key, $data, $expire = 3600)\n\t    {\n\t        $this-\u003edata[$message_type][$key][$sub_key] = $data;\n\t    }\n\t\n\t    public function get($message_type, $key, $sub_key)\n\t    {\n\t        return $this-\u003edata[$message_type][$key][$sub_key];\n\t    }\n\t\n\t    public function delete($message_type, $key, $sub_key)\n\t    {\n\t        if (!isset($this-\u003edata[$message_type][$key][$sub_key])){\n\t            echo \"storage not found:$message_type $key $sub_key\";\n\t        }\n\t        unset($this-\u003edata[$message_type][$key][$sub_key]);\n\t    }\n\n\t}\n\nuse MqttClient\n\n\t$host = '127.0.0.1';\n\t$port = 1883;\n\n\t$r = new \\mqttclient\\src\\swoole\\MqttClient($host,$port,10017);\n\t$r-\u003esetAuth('username','password');\n\t$r-\u003esetKeepAlive(60);\n\t$r-\u003esetLogger(new Logger());\n\t$r-\u003esetStore(new Store());\n    //dns lookup\n    $r-\u003esetDnsLookup(true);\n    //buffer size\n    $r-\u003esetSocketBufferSize(1024*1024*5);\n    //reconnect times when error\n    $r-\u003esetMaxReconnectTimesWhenError(360*12);\n    //reconnect interval\n    $r-\u003esetReconnectInterval(10000);\n    //subscribe topics,callback's params can be any data we mapped into the container(IOC)\n\t$r-\u003esetTopics(\n    [\n        new \\mqttclient\\src\\subscribe\\Topic('test/slim',function($msg){\n            echo \"I receive:\".$msg.\"\\r\\n\";}),\n        new \\mqttclient\\src\\subscribe\\Topic('test/slim3',function(\\mqttclient\\src\\swoole\\MqttClient $client,$msg){\n            echo \"I receive:\".$msg.\" for slim3 \\r\\n\";\n            echo $client-\u003egetClientId();\n        })\n    ]\n\t);\n\t\n\t//set trigger\n\t$r-\u003eon(\\mqttclient\\src\\consts\\ClientTriggers::RECEIVE_SUBACK,function(\\mqttclient\\src\\swoole\\MqttClient $client){\n    \t$client-\u003epublish('slim/echo','GGXX',\\mqttclient\\src\\consts\\Qos::ONE_TIME);\n    });\n\t\n\t$r-\u003econnect();\n\t$r-\u003epublish('test/slim','test qos',2);\n\t\n### Extends\n\nYou can also use own client extends MqttClient.\n\nExample:\n\n\tclass Client extends MqttClient\n\t{\n\t    private $mysql_handler;\n\t    private $mongo_handler;\n\t\n\t    public function __construct($host,$port,$client_id,$mysql_conf,$mongo_conf)\n\t    {\n\t        $this-\u003emysql_handler = new Mysqli($mysql_conf);\n\t        $this-\u003emongo_handler = new \\MongoClient('mongodb://'.$mongo_conf['username'].':'.$mongo_conf['password'].'@'.$mongo_conf['host'].':'.$mongo_conf['port'].'/'.$mongo_conf['db']);\n\t        parent::__construct($host,$port,$client_id);\n\t    }\n\t\n\t\t /**\n\t     * override the produceContainer function and map your own class/data/closure to the injector,and they can be used in every publish receive handler\n\t     * for exp: $client-\u003esetTopics([new Topic('test/own',function($mongo,$msg){ $result = $mongo-\u003eselectCollection('log_platform','test')-\u003efind(['sid' =\u003e ['$gte' =\u003e intval($msg)]]); })]);\n\t     * @return Injector\n\t     */\n\t    protected function produceContainer()\n\t    {\n\t        $container = new Injector();\n\t        $container-\u003emapData(MqttClient::class,$this);\n\t        $container-\u003emapData(Client::class,$this);\n\t        $container-\u003emapData('mysqli',$this-\u003emysql_handler);\n\t        $container-\u003emapData('mongo',$this-\u003emongo_handler);\n\t        return $container;\n\t    }\n\t\n\t}\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjesusslim%2Fmqttclient","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjesusslim%2Fmqttclient","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjesusslim%2Fmqttclient/lists"}