{"id":27962822,"url":"https://github.com/php-mqtt/client-examples","last_synced_at":"2025-05-07T19:57:23.775Z","repository":{"id":46232429,"uuid":"208608271","full_name":"php-mqtt/client-examples","owner":"php-mqtt","description":"These are examples showcasing how to use php-mqtt/client.","archived":false,"fork":false,"pushed_at":"2021-11-04T19:35:07.000Z","size":26,"stargazers_count":24,"open_issues_count":0,"forks_count":17,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-05-07T19:57:18.760Z","etag":null,"topics":[],"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/php-mqtt.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-09-15T14:44:14.000Z","updated_at":"2025-02-27T20:59:26.000Z","dependencies_parsed_at":"2022-07-26T05:16:46.242Z","dependency_job_id":null,"html_url":"https://github.com/php-mqtt/client-examples","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-mqtt%2Fclient-examples","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-mqtt%2Fclient-examples/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-mqtt%2Fclient-examples/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-mqtt%2Fclient-examples/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/php-mqtt","download_url":"https://codeload.github.com/php-mqtt/client-examples/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252949307,"owners_count":21830150,"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":[],"created_at":"2025-05-07T19:57:23.238Z","updated_at":"2025-05-07T19:57:23.769Z","avatar_url":"https://github.com/php-mqtt.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# php-mqtt/client-examples\n\nThis repository contains examples for [`php-mqtt/client`](https://github.com/php-mqtt/client).\n\n## Available Examples\n\n### 1) Publishing\n\nWhen publishing, there is only a noteworthy difference between QoS 0 and QoS 1/2, since QoS 0 is fire and forget,\nwhile QoS 1/2 require confirmation. For completeness, an example for each QoS level is provided.\n\n- [Publish a message using QoS 0](01_publishing/01_publish_with_qos_0.php) (_unreliable delivery_)\n- [Publish a message using QoS 1](01_publishing/02_publish_with_qos_1.php) (_reliable delivery, but probably multiple times_)\n- [Publish a message using QoS 2](01_publishing/03_publish_with_qos_2.php) (_reliable delivery, exactly once_)\n\n### 2) Subscribing\n\nWhen subscribing, there is not really a difference between the QoS levels. All three QoS levels work the same from a library API perspective.\nUnder the hood, each of the QoS levels uses a slightly different implementation, though, to follow the protocol specification.\nSame as with publishing, an example for each QoS level is provided for completeness.\n\n- [Subscribe to a topic using QoS 0](02_subscribing/01_subscribe_with_qos_0.php) (_unreliable delivery_)\n- [Subscribe to a topic using QoS 1](02_subscribing/02_subscribe_with_qos_1.php) (_reliable delivery, but probably multiple times_)\n- [Subscribe to a topic using QoS 2](02_subscribing/03_subscribe_with_qos_2.php) (_reliable delivery, exactly once_)\n\n### 3) Connection Settings\n\nFor the connection to the broker, additional settings can be used. For example a username and password for authorization,\na customized timeout for the connection attempt or TLS settings for a secured connection.\nFor simplicity, all the following examples will publish a single message using QoS 0 after connecting to the broker.\n\n- [Authorize using username and password](03_connection_settings/01_authorize_with_username_and_password.php)\n- [Use TLS without client certificate](03_connection_settings/02_use_tls_without_client_certificate.php)\n- [Use TLS with client certificate](03_connection_settings/03_use_tls_with_client_certificate.php)\n- [Declare Last Will upon connection](03_connection_settings/04_declare_last_will_upon_connection.php)\n\n### 4) Hooks\n\nTo inject logic into the execution path of our MQTT client, it is possible to use so-called hooks.\nThey are essentially callbacks which can be registered for a specific purpose and which are called upon certain events.\nThe following gives a few examples and ideas what can be done with hooks, although they are a lot more versatile than can be shown here.\n\n- [Use a loop event handler to perform periodic actions](04_hooks/01_use_a_loop_event_handler_to_perform_periodic_actions.php)\n- [Use a publish event handler to log published messages](04_hooks/02_use_a_publish_event_handler_to_log_published_messages.php)\n- [Use a message received event handler to log received messages](04_hooks/03_use_a_message_received_event_handler_to_log_received_messages.php)\n- [Use a loop event handler to timeout a remote procedure call](04_hooks/04_use_a_loop_event_handler_to_timeout_a_remote_procedure_call.php)\n\n### 5) Interrupting the Loop\n\nSince the event loop provided by `MqttClient::loop()` is an infinite loop by design, most applications need a way to escape it.\nMost often the primary use case is for a graceful shutdown of the application, to avoid forceful termination.\n\n- [Use `pcntl_signal` to interrupt the loop](05_interuppting_the_loop/01_use_pcntl_signal_to_interrupt_the_loop.php)\n\n## How to run the examples?\n\nSimply clone the repository and run `composer install` to install the required dependencies.\nYou can then run the examples one by one, but please don't forget to change the shared settings like the MQTT broker host and port before.\nThe shared settings can be found in [`shared/config.php`](shared/config.php). Alternatively, the examples can be altered directly.\n\n### Noteworthy\n\nThe examples use a custom logger to give insight about what is happening internally. You can adjust the logging level as needed.\n\n## License\n\n`php-mqtt/client-examples` is open-source software licensed under the [MIT license](LICENSE.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphp-mqtt%2Fclient-examples","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphp-mqtt%2Fclient-examples","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphp-mqtt%2Fclient-examples/lists"}