PHP code example of php-mqtt / laravel-client
1. Go to this page and download the library: Download php-mqtt/laravel-client library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
php-mqtt / laravel-client example snippets
'default_connection' => 'private',
'connections' => [
'private' => [
'host' => 'mqtt.example.com',
'port' => 1883,
],
'public' => [
'host' => 'test.mosquitto.org',
'port' => 1883,
],
],
use PhpMqtt\Client\Facades\MQTT;
MQTT::publish('some/topic', 'Hello World!');
use PhpMqtt\Client\Facades\MQTT;
MQTT::publish('some/topic', 'Hello World!', true, 'public');
use PhpMqtt\Client\Facades\MQTT;
/** @var \PhpMqtt\Client\Contracts\MqttClient $mqtt */
$mqtt = MQTT::connection();
$mqtt->publish('some/topic', 'foo', 1);
$mqtt->publish('some/other/topic', 'bar', 2, true); // Retain the message
$mqtt->loop(true);
/** @var \PhpMqtt\Client\Contracts\MqttClient $mqtt */
$mqtt->loop(true, true);
/** @var \PhpMqtt\Client\Contracts\MqttClient $mqtt */
pcntl_signal(SIGINT, function () use ($mqtt) {
$mqtt->interrupt();
});
use PhpMqtt\Client\Facades\MQTT;
/** @var \PhpMqtt\Client\Contracts\MqttClient $mqtt */
$mqtt = MQTT::connection();
$mqtt->subscribe('some/topic', function (string $topic, string $message) {
echo sprintf('Received QoS level 1 message on topic [%s]: %s', $topic, $message);
}, 1);
$mqtt->loop(true);
bash
composer
bash
php artisan vendor:publish --provider="PhpMqtt\Client\MqttClientServiceProvider" --tag="config"