GithubHelp home page GithubHelp logo

mqtt-client's Introduction

MQTT Client

This is an MQTT client package to connect to and disconnect from an MQTT broker as well as publish data and subscribe to and unsubscribe to topics.

This mqtt client uses ReactPHP at its core to implement its asynchronous nature and takes inspiration from alexmorbo/react-mqtt and oliverlorenz/phpMqttClient.

Goal

Goal of this project is easy to use MQTT client for PHP in a modern architecture without using any php modules.

The table shows the versions implemented in this library.

Version Specification
Version 3.1.1 http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/csprd02/mqtt-v3.1.1-csprd02.html

Installation

You can install the package via composer by running:

composer require "alhaji-aki/mqtt-client"

Usage

This client can be used in any php application. Check out the 'usage' directory to find example codes.

initial setup

<?php

use AlhajiAki\Mqtt\Client;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;

require_once __DIR__ . '/../vendor/autoload.php';

$logger = new Logger('logs', [
    new StreamHandler(__DIR__ . '/../logs/app.log')
]);

$client = new Client('127.0.0.1:1883', [
    'client_id' => 'newclient',
    // 'keep_alive' => 120,
    // 'username' => 'auth_user',
    // 'password' => 'auth_password',
    // 'clean_session' => true, // default is true
    // 'last_will_topic' => '',
    // 'last_will_message' => '',
    // 'last_will_qos' => '',
    // 'last_will_retain' => ''
], $logger);

connect

connecting to a broker/server

<?php

require_once __DIR__ . '/index.php';

$connection = $client->connect();

$client->loop()->run();

disconnect

disconnecting from a broker/server

<?php

use React\Socket\ConnectionInterface;

require_once __DIR__ . '/index.php';

$connection = $client->connect();

$connection->then(function (ConnectionInterface $stream) use ($client) {
    $client->disconnect($stream);
});

$client->loop()->run();

publishing data

publishing data to the broker

<?php

use AlhajiAki\Mqtt\Qos\Levels;
use React\Socket\ConnectionInterface;

require_once __DIR__ . '/index.php';

$connection = $client->connect();

$connection->then(function (ConnectionInterface $stream) use ($client) {
    $data = [
        'foo' => 'bar',
        'bar' => 'baz',
        'time' => time(),
    ];

    $qos = Levels::AT_MOST_ONCE_DELIVERY;  // 0

    $client->publish($stream, 'foo/bar', json_encode($data), $qos)
        ->then(function (ConnectionInterface $stream) use ($client) {
            /**
             * Disconnect when published
             */
            $client->disconnect($stream);
        });
});

$client->loop()->run();

subscribing to topics

Subscribing to a single topic

<?php

use AlhajiAki\Mqtt\Packets\Publish;
use React\Socket\ConnectionInterface;

require_once __DIR__ . '/index.php';

$connection = $client->connect();

$connection->then(function (ConnectionInterface $stream) use ($client) {
    $client->subscribe($stream, 'foo/bar', 0)->then(function (ConnectionInterface $stream) use ($qos) {
        // Success subscription
        $stream->on(Publish::EVENT, function (Publish $publish) {
            var_dump($publish->getTopic());
            var_dump($publish->getRetain());
            var_dump($publish->getDup());
            var_dump($publish->getQos());
            var_dump($publish->getPayload());
            var_dump($publish->getPacketId());
        });
    });
});

$client->loop()->run();

Subscribing to multiple topics

<?php

use AlhajiAki\Mqtt\Packets\Publish;
use React\Socket\ConnectionInterface;

require_once __DIR__ . '/index.php';

$connection = $client->connect();

$connection->then(function (ConnectionInterface $stream) use ($client) {
    $topics = [
        [
            'topic' => 'foo/bar',
            'qos' => 1
        ],
        [
            'topic' => 'test',
            'qos' => 0
        ],
        [
            'topic' => 'foo/bas',
            'qos' => 2
        ]
    ];
    $client->subscribe($stream, $topics)->then(function (ConnectionInterface $stream) use ($qos) {
        // Success subscription
        $stream->on(Publish::EVENT, function (Publish $publish) {
            var_dump($publish->getTopic());
            var_dump($publish->getRetain());
            var_dump($publish->getDup());
            var_dump($publish->getQos());
            var_dump($publish->getPayload());
            var_dump($publish->getPacketId());
        });
    });
});

$client->loop()->run();

unsubscribing from topics

Unsubscribing from a single topic

<?php

use AlhajiAki\Mqtt\Packets\Publish;
use React\Socket\ConnectionInterface;

require_once __DIR__ . '/index.php';

$connection = $client->connect();

$connection->then(function (ConnectionInterface $stream) use ($client) {
    $client->unsubscribe($stream, 'foo/bar', 0)->then(function (ConnectionInterface $stream) use ($qos) {
        /**
         * Disconnect when published
         */
        $client->disconnect($stream);
    });
});

$client->loop()->run();

Unsubscribing from multiple topics

<?php

use AlhajiAki\Mqtt\Packets\Publish;
use React\Socket\ConnectionInterface;

require_once __DIR__ . '/index.php';

$connection = $client->connect();

$connection->then(function (ConnectionInterface $stream) use ($client) {
    $topics = ['foo/bar', 'test','foo/bas'];

    $client->unsubscribe($stream, $topics)->then(function (ConnectionInterface $stream) use ($qos) {
        /**
         * Disconnect when published
         */
        $client->disconnect($stream);
    });
});

$client->loop()->run();

Contributing

Please see CONTRIBUTING for details.

License

The MIT License (MIT). Please see License File for more information.

mqtt-client's People

Contributors

alhaji-aki avatar

Watchers

James Cloos avatar  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.