Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

aosdev's avatar

Laravel with MQTT

Hey,

I have a device and it send the current temperature and humidity every minute where it located to MQTT cloud (HiveMQ). I found a package "mqtt client" for PHP. Also a guy build his own "mqtt laravel client". I added this package to my project and using it on my local, currently i'm displaying these data on terminal. The point is, i have to run a terminal for (subscribe) recieving data.

The code looks like below;

<?php

require('vendor/autoload.php');

use \PhpMqtt\Client\MqttClient;
use \PhpMqtt\Client\ConnectionSettings;

$server   = 'xxx.com';
$port     = 8883;
$clientId = 'xxx';
$username = 'xxx';
$password = 'xxx';
$clean_session = true;
$mqtt_version = MqttClient::MQTT_3_1;

$connectionSettings = (new ConnectionSettings)
    ->setConnectTimeout(10)
    ->setUsername($username)
    ->setPassword($password)
    ->setUseTls(true)
    ->setTlsSelfSignedAllowed(true)
    ->setKeepAliveInterval(60);


$mqtt = new MqttClient($server, $port, $clientId, $mqtt_version);

$mqtt->connect($connectionSettings, $clean_session);
printf("client connected\n");

$mqtt->subscribe('#', function ($topic, $message) {
    printf("Received message on topic [%s]: %s\n", $topic, $message);
}, 0);

$mqtt->loop(true);

I'm trying to find out, how can i use it on Laravel project (i mean cronjobs or anyting else), always listen the server and when the data recieved it saves to database.

Kind regards.

0 likes
3 replies
LaryAI's avatar
Level 58

To use MQTT in a Laravel project, you can create a command that runs in the background and listens for incoming messages. Here's an example:

  1. Create a new command using the make:command Artisan command:
php artisan make:command MqttListener
  1. Open the app/Console/Commands/MqttListener.php file and replace the handle method with the following code:
public function handle()
{
    $server = 'xxx.com';
    $port = 8883;
    $clientId = 'xxx';
    $username = 'xxx';
    $password = 'xxx';
    $clean_session = true;
    $mqtt_version = MqttClient::MQTT_3_1;

    $connectionSettings = (new ConnectionSettings)
        ->setConnectTimeout(10)
        ->setUsername($username)
        ->setPassword($password)
        ->setUseTls(true)
        ->setTlsSelfSignedAllowed(true)
        ->setKeepAliveInterval(60);

    $mqtt = new MqttClient($server, $port, $clientId, $mqtt_version);

    $mqtt->connect($connectionSettings, $clean_session);
    printf("client connected\n");

    $mqtt->subscribe('#', function ($topic, $message) {
        printf("Received message on topic [%s]: %s\n", $topic, $message);
        // Save the message to the database
    }, 0);

    $mqtt->loop(true);
}
  1. Replace the MQTT connection settings with your own.

  2. In the subscribe callback function, you can save the received message to the database using Laravel's Eloquent ORM or any other database library.

  3. To run the command in the background, you can use a process manager like Supervisor or systemd. Here's an example configuration for Supervisor:

[program:mqtt-listener]
command=php /path/to/artisan mqtt:listen
autostart=true
autorestart=true
user=www-data
redirect_stderr=true
stdout_logfile=/path/to/logs/mqtt-listener.log
  1. Start the Supervisor process:
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start mqtt-listener

Now the mqtt:listen command will run in the background and listen for incoming messages.

1 like

Please or to participate in this conversation.