Looking to hire Laravel developers? Try LaraJobs

laravel-mqtt-client maintained by alhaji-aki

Description
A package to subscribe and publish to mqtt protocols
Author
Last update
2021/05/30 15:26 (dev-master)
License
Links
Downloads
3

Comments
comments powered by Disqus

installation

composer require alhaji-aki/laravel-mqtt-client

publish config and adds listen command to app

php artisan mqtt:install

change the logic in the listen command and register it in the Command Kernel

available methods:

connect to a broker

MqttClient::connect();

get connection

MqttClient::connect()->connection();

disconnect

use AlhajiAki\MqttClient\Facades\MqttClient;

$connection = MqttClient::connect()->connection();
$connection->disconnect();

publish

use AlhajiAki\MqttClient\Facades\MqttClient;

$data = [
    'foo' => 'bar',
    'bar' => 'baz',
    'time' => time(),
]

MqttClient::connect()->publish('foo/bar', json_encode($data), 0);

subscribe

use AlhajiAki\MqttClient\Facades\MqttClient;
use Morbo\React\Mqtt\Packets\Publish;

MqttClient::connect()->subscribe('foo/bar', function ($stream) {
    $stream->on(Publish::EVENT, function (Publish $message) {
        printf(
            'Received payload "%s" for topic "%s"%s',
            $message->getPayload(),
            $message->getTopic(),
            PHP_EOL
        );
    });
}, 0);

unsubscribe

use AlhajiAki\MqttClient\Facades\MqttClient;

MqttClient::connect()->unsubscribe('foo/bar', function ($message) {
    printf(
        'Received payload "%s" for topic "%s"%s',
        $message->getPayload(),
        PHP_EOL
    );
}, function ($ex) {
    printf('Error occured: %s', $ex->getMessage());
});