PHP code example of inbll / mqtt

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

    

inbll / mqtt example snippets

demo


use Inbll\Mqtt\Broker;


$config = [
    'database' => 'redis',

    /**
     * protocol version in 3.1、3.1.1
     */
    'version' => '3.1.1',

    /**
     * port
     */
    'port' => 1883,

    /**
     * swoole config
     */
    'swoole' => [
        'worker_num' => 2,

        'task_worker_num' => 2,

        'task_enable_coroutine' => true,

        'heartbeat_check_interval' => 10,
        'heartbeat_idle_time' => 120,
    ],

    /**
     * database configs
     */
    'databases' => [
        'redis' => [
            'host' => '127.0.0.1',
            'password' => null,
            'port' => 6379,
            'database' => 0,
            'prefix' => 'mqtt_'
        ]
    ],
];

$broker = new Broker($config);

$broker->on('start', function (Broker $broker) {
    $broker->log('start...');
});

$broker->on('connected', function (Broker $broker, string $clientId) {
    $$broker->log('hello ' . $clientId);
});

$broker->on('message', function (Broker $broker, string $clientId, string $topic, string $message) {
    $broker->publish($clientId, 'test', 'hello', 2);
    
    $broker->close($clientId);
});

$broker->start();