PHP code example of vs-point / messenger-transport-mqtt

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

    

vs-point / messenger-transport-mqtt example snippets




namespace App;

use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;

class Scenario {

    /** @var MessageBusInterface  */
    private $bus;

    public function __construct(MessageBusInterface $bus)
    {
        $this->bus = $bus;
    }

    public function __invoke(string $section = 'ALL')
    {
        $this->bus->dispatch(new StateMessage($section, 'newState'));
    }

}



namespace App;

use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;

class Controller {

    /** @var MessageBusInterface  */
    private $bus;

    /** @var RouterInterface */
    private $router;

    /** @var Scenario  */
    private $stateScenario;

    public function __construct(MessageBusInterface $bus, RouterInterface $router, Scenario $stateScenario)
    {
        $this->bus = $bus;
        $this->router = $router;
        $this->stateScenario = $stateScenario;
    }

    /**
     * @Route("/state/{section}",
     *     name="change.state",
     *     



namespace App;

use VSPoint\Messenger\Transport\Mqtt\MqttMessage;
use VSPoint\Messenger\Transport\Mqtt\MqttMessageInterface;

class StateMessage implements MqttMessageInterface
{
    public function __construct(string $section, string $state)
    {
        $this->topic = '/state/'.$section;
        $this->message = $state;
    }

    private $topic;
    private $message;

    public function getTopic(): string
    {
        return $this->topic;
    }

    public function getQos(): int
    {
        return 1;
    }

    public function getBody(): string
    {
        return $this->message;
    }

}