summergeorge/laravel-mqttx

A simple PHP Laravel framework MQTT library, very convenient to use

1.1.0 2020-09-04 10:57 UTC

This package is auto-updated.

Last update: 2024-11-05 19:35:23 UTC


README

A simple Laravel MQTT client provides Publish/Subscribe methods.

Source code is mainly based on phpMQTT.

Once the configuration is complete, it is ready to use.

install

$ composer require jzzoo/laravel-mqttx -vvv
$ php artisan vendor:publish

config

config/mqttx.php

return [
    'host'     => env('mqtt_host','127.0.0.1'),
    'password' => env('mqtt_password',''),
    'username' => env('mqtt_username',''),
    'certfile' => env('mqtt_cert_file',''),
    'port'     => env('mqtt_port','1883'),
    'debug'    => env('mqtt_debug',false),
    'qos'      => env('mqtt_qos', 0),
    'retain'   => env('mqtt_retain', 0)
];

example:

$mqtt = new Mqttx();

// Post news to test/topic topic
$mqtt->Publish('test/topic', sprintf("mqtt message: %d", mt_rand(10,9999)));
   
   
// Subscribe to test/topic topic       
$mqtt->Subscribe('test/topic', function ($topic, $message) {
    dump($topic);
    dump($message);
});

If Debug is turned on and Publish is called on the browser, you can suppress the output using ob_get_clean().