composer require php-mqtt/client
<?php
require __DIR__.'/vendor/autoload.php';
use PhpMqtt\Client\MqttClient;
$client = new MqttClient('127.0.0.1', 1883, 'client1');
$client->connect();
$client->publish('topic', 'Hello World!');
$client->disconnect();
<?php
require __DIR__.'/vendor/autoload.php';
use PhpMqtt\Client\MqttClient;
$client = new MqttClient('127.0.0.1', 1883, 'client2');
$client->connect();
$client->subscribe('topic', function($topic, $message) {
echo $topic, $message, PHP_EOL;
});
$client->loop(true);
$client->disconnect();