Git Product home page Git Product logo

client-examples's Introduction

php-mqtt/client-examples

This repository contains examples for php-mqtt/client.

Available Examples

1) Publishing

When publishing, there is only a noteworthy difference between QoS 0 and QoS 1/2, since QoS 0 is fire and forget, while QoS 1/2 require confirmation. For completeness, an example for each QoS level is provided.

2) Subscribing

When subscribing, there is not really a difference between the QoS levels. All three QoS levels work the same from a library API perspective. Under the hood, each of the QoS levels uses a slightly different implementation, though, to follow the protocol specification. Same as with publishing, an example for each QoS level is provided for completeness.

3) Connection Settings

For the connection to the broker, additional settings can be used. For example a username and password for authorization, a customized timeout for the connection attempt or TLS settings for a secured connection. For simplicity, all the following examples will publish a single message using QoS 0 after connecting to the broker.

4) Hooks

To inject logic into the execution path of our MQTT client, it is possible to use so-called hooks. They are essentially callbacks which can be registered for a specific purpose and which are called upon certain events. The 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.

5) Interrupting the Loop

Since the event loop provided by MqttClient::loop() is an infinite loop by design, most applications need a way to escape it. Most often the primary use case is for a graceful shutdown of the application, to avoid forceful termination.

How to run the examples?

Simply clone the repository and run composer install to install the required dependencies. You 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. The shared settings can be found in shared/config.php. Alternatively, the examples can be altered directly.

Noteworthy

The examples use a custom logger to give insight about what is happening internally. You can adjust the logging level as needed.

License

php-mqtt/client-examples is open-source software licensed under the MIT license.

client-examples's People

Contributors

namoshek avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.