PHP code example of try-to / swoole_mqtt

1. Go to this page and download the library: Download try-to/swoole_mqtt 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/ */

    

try-to / swoole_mqtt example snippets



use TrytoMqtt\Client;
on' => false,
    'client_id' => 'demo-subscribe-123456',
    'username' => '',
    'password' => '',
];
$mqtt = new Client('127.0.0.1', 1883, $options);
$mqtt->onConnect = function ($mqtt) {
    $mqtt->subscribe('/World');
};
$mqtt->onMessage = function ($topic, $content) {
    var_dump($topic, $content);
};
$mqtt->onError = function ($exception) use ($mqtt) {
    echo "error\n";
    // $mqtt->reconnect(1000);
};
$mqtt->onClose = function () {
    echo "close\n";
};
$mqtt->connect();
 subscribe.php


use TrytoMqtt\Client;
on' => false,
    'client_id' => 'demo-publish-123456',
    'username' => '',
    'password' => '',
];
$mqtt = new Client('127.0.0.1', 1883, $options);
$mqtt->onConnect = function ($mqtt) {
    $mqtt->publish('/World', 'hello swoole mqtt');
};
$mqtt->onError = function ($exception) {
    echo "error\n";
};
$mqtt->onClose = function () {
    echo "close\n";
};
$mqtt->connect();
 publish.php