{"id":14963519,"url":"https://github.com/markkimsal/amphp-mqtt","last_synced_at":"2025-10-25T02:31:00.438Z","repository":{"id":62524208,"uuid":"133080382","full_name":"markkimsal/amphp-mqtt","owner":"markkimsal","description":"MQTT client for Amp","archived":false,"fork":false,"pushed_at":"2018-05-23T21:12:21.000Z","size":58,"stargazers_count":5,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-06T11:50:40.404Z","etag":null,"topics":["amphp","async","message-queue","mqtt","php"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/markkimsal.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-05-11T19:26:48.000Z","updated_at":"2024-05-31T08:00:43.000Z","dependencies_parsed_at":"2022-11-02T15:30:53.383Z","dependency_job_id":null,"html_url":"https://github.com/markkimsal/amphp-mqtt","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markkimsal%2Famphp-mqtt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markkimsal%2Famphp-mqtt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markkimsal%2Famphp-mqtt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/markkimsal%2Famphp-mqtt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/markkimsal","download_url":"https://codeload.github.com/markkimsal/amphp-mqtt/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238065137,"owners_count":19410587,"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":["amphp","async","message-queue","mqtt","php"],"created_at":"2024-09-24T13:31:44.239Z","updated_at":"2025-10-25T02:30:55.121Z","avatar_url":"https://github.com/markkimsal.png","language":"PHP","funding_links":[],"categories":["Unsorted yet"],"sub_categories":["Tunnel"],"readme":"# MQTT\n\n`markkimsal/amphp-mqtt` is an asynchronous MQTT client for PHP based on Amp.\n\n## Installation\n\n```\ncomposer require markkimsal/amphp-mqtt\n```\n\nThis project does not require any PHP extensions.\n\n## Usage\n\nEvery publish or subscribe returns an `Amp\\Promise` that you can react to when it's resolved.  Messages are received from `-\u003eon('message')` and that takes a callback with only 1 argument: the `Packet\\Publish` class.  `Packet\\Publish` is used for both sending and receiving messages.\n\n\nYou can start sending and subscribing before the connection ack packet is received and the system will queue up your packets but still return a `Amp\\Promise` immediately.\n\nQoS 0 packets will resolve as soon as they are sent as they will not get any acknowledgement from the server.\n\nQoS 1 packets will resolve when the client gets a Puback packet.\n\nQoS 2 packets will resolve when the client gets a Pubcomp packet.  The client will automatically respond to Pubrec with a Pubrel as per the spec.\n\nSupports TLSv1.2 by using \"tls://\" prefix to the URI of the server.  Peer verification is not done.\n\nSupports username and password authentication as parameters of the connection URL.\n\n```php\n\u003c?php\ninclude('vendor/autoload.php');\nuse \\Amp\\Loop;\nuse \\MarkKimsal\\Mqtt\\Client;\n\nLoop::run( function($w) {\n\n\t$client = new Client('tcp://172.17.0.1:1883?topics=foo,bar\u0026clientId=abc123');\n\n\t$p = $client-\u003econnect();\n\n\t$p2 = $client-\u003esubscribe('test/', function($err, $resp) {\n\t\techo \"***** SUBSCRIBE Resolved *******\\n\";\n\t\tvar_dump($err);\n\t\tvar_dump($resp);\n\t});\n\n\t$p-\u003eonResolve(function($err, $resp) use($p, $client){\n\t\techo \"****** CONNECT Resolved ********\\n\";\n\t});\n\n\t$p2-\u003eonResolve(function($err, $res) {\n\t\techo \"***** SUBSCRIBE Resolved in a different way *******\\n\";\n\t\tvar_dump($err);\n\t\tvar_dump($res);\n\t});\n\n\t$client-\u003eon('message', function($publishPacket) {\n\t\techo \"****** got a message on topic: [\".$publishPacket-\u003egetTopic().\"] ***** \\n\";\n\t\techo $publishPacket-\u003egetMessage().\"\\n\";\n\t});\n\n\tLoop::repeat(1000, function() use($client){\n\t\t$client-\u003epublish('Current time is: '.date('H:i:s'), 'time', 0, function($err, $result) {\n\t\t\tif (!$err) {\n\t\t\t\techo \"***** Socket fired off Publish Packet with qos 0 *****\\n\";\n\t\t\t}\n\t\t});\n\t});\n});\n```\n\nBlocking mode (integration with sync code)\n===\nYou can use this library in a synchronous backend to wait for sending packets with QoS 0,1, or 2.\n\n```php\n\u003c?php\n\tinclude('vendor/autoload.php');\n\n\t$client = new MarkKimsal\\Mqtt\\Client('tcp://172.17.0.1:1883');\n\n\t$pconn = $client-\u003econnect();\n\n\t$p0 = $client-\u003epublishRetain('QoS0 Current time is: '.date('H:i:s'), 'time', 0, function($err, $result) {\n\t\tif (!$err) {\n\t\t\techo \"***** Socket fired off Publish Packet with qos 0 *****\\n\";\n\t\t}\n\t});\n\n\t$p1 = $client-\u003epublish('QoS1 Current time is: '.date('H:i:s'), 'time', 1, function($err, $result) {\n\t\tif (!$err) {\n\t\t\techo \"***** Got Publish Ack with qos 1 *****\\n\";\n\t\t}\n\t});\n\n\t$p2 = $client-\u003epublish('QoS2 Current time is: '.date('H:i:s'), 'time', 2, function($err, $result) {\n\t\tif (!$err) {\n\t\t\techo \"***** Got Publish Ack with qos 2 *****\\n\";\n\t\t}\n\t});\n\n\n\tAmp\\Promise\\wait($p0);\n\tAmp\\Promise\\wait($p1);\n\tAmp\\Promise\\wait($p2);\n\treturn;\n```\n\nManual Publish Acking\n===\nYou can control when you acknowlege publish messages of QoS 1 and 2 by disabling the auto-ack feature.\nOtherwise, the appropriate acknowledgement packets will be sent after the `on('message')` handler is run.\n\n```php\n\u003c?php\n\tinclude('vendor/autoload.php');\n\n\t$client = new MarkKimsal\\Mqtt\\Client('tcp://172.17.0.1:1883?topics=foo,bar\u0026clientId=abc123');\n\t$client-\u003edisableAutoAck();\n\n\t$p2 = $client-\u003esubscribe('test/#', function($err, $resp) {\n\t\techo \"***** SUBSCRIBE Resolved *******\\n\";\n\t});\n\n\n\t$p = $client-\u003econnect();\n\n\t$client-\u003eon('message', function($publishPacket) use($client) {\n\t\tif ($publishPacket-\u003eisDup()) {\n\t\t\techo \"****** got a DUP on topic: [\".$publishPacket-\u003egetTopic().\"] ***** \\n\";\n\t\t\techo $publishPacket-\u003egetMessage().\"\\n\";\n\t\t} else {\n\t\t\techo \"****** got a message on topic: [\".$publishPacket-\u003egetTopic().\"] ***** \\n\";\n\t\t\techo $publishPacket-\u003egetMessage().\"\\n\";\n\t\t}\n\n\t\t//save message with durability here\n\n\t\t$client-\u003eacknowledge($publishPacket);\n\t});\n```\n\nClean Session\n===\nYou can connect with a clean session by adding `cleanSession` as a URL parameter.\n\nYou MUST either use a clean session or supply a client ID.\n\nIf you do not supply a clientId a clean session is automatically created for you.\n\n```php\n\tinclude('vendor/autoload.php');\n\n\t$client = new MarkKimsal\\Mqtt\\Client('tcp://172.17.0.1:1883?cleanSession');\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarkkimsal%2Famphp-mqtt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarkkimsal%2Famphp-mqtt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarkkimsal%2Famphp-mqtt/lists"}