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\Message;
use VSPoint\Messenger\Transport\Mqtt\MqttMessageInterface;
final class SensorReading implements MqttMessageInterface
{
public function __construct(
private readonly string $sensorId,
private readonly float $value,
) {
}
public function getTopic(): string
{
return '/sensors/' . $this->sensorId;
}
public function getQos(): int
{
return 1; // at least once
}
public function getBody(): string
{
return (string) $this->value;
}
}
namespace App\MessageHandler;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
use VSPoint\Messenger\Transport\Mqtt\MqttMessage;
#[AsMessageHandler]
final class MqttMessageHandler
{
public function __invoke(MqttMessage $message): void
{
// $message->getTopic() — MQTT topic the message arrived on
// $message->getBody() — raw payload string
// $message->getQos() — QoS level (always 0 for received messages)
// $message->getId() — unique ID generated on receipt
}
}
bash
php bin/console messenger:consume mqtt
bash
docker compose run --rm php ./vendor/bin/phpunit --testdox
bash
docker compose run --rm php ./vendor/bin/phpstan analyse --memory-limit=512M
bash
# check
docker compose run --rm php ./vendor/bin/ecs check
# fix
docker compose run --rm php ./vendor/bin/ecs check --fix
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.