Real-Time IoT Messaging

Laravel MQTT

A high-performance MQTT client implementation for Laravel, allowing robust publish/subscribe messaging for IoT devices and real-time backend communication.

Open SourceLaravelIoTMQTT
Thermostatsensor/temp
TemperatureUpdatedEvent sensor/temp
Gateway 1device/1/data
ProcessDeviceDataJob device/#
Room 12room/12/lights
ProcessLightCommandJob room/+/lights
Gateway 1device/1/cmd
Mqtt::publishPublish ['reboot' => true]
Mqtt::publish('device/1/cmd', ['reboot' => true])
  1. 09:41:05room/12/lights{"on":true}
  2. 09:41:04device/1/data{"battery":87}
  3. 09:41:03sensor/temp{"celsius":22.4}
Illustrative topology with sample devices and messages; topics and handlers are the README's examples
Topic routing

Type a topic, see where it goes

Routes are registered in a service provider's boot method. Exact topics and both MQTT wildcards are supported: + matches one level, # matches the rest.

Matches 1 route: room/+/lights

  • Mqtt::topic('sensor/temp')Exact match, dispatches TemperatureUpdated · no matchEvent
  • Mqtt::topic('device/#')Multi-level wildcard, queues ProcessDeviceData · no matchJob
  • Mqtt::topic('room/+/lights')Single-level wildcard, queues ProcessLightCommand · matchJob
Code

Publish in one line, subscribe in three

Publish

Arrays are JSON-encoded automatically.

Publishing
use Haiderkamran\LaravelMqtt\Facades\Mqtt;
use Haiderkamran\LaravelMqtt\Enums\QosLevel;
 
Mqtt::publish('device/1/cmd', ['reboot' => true]);
 
// With QoS and Retain flag
Mqtt::publish('device/status', 'online', QosLevel::EXACTLY_ONCE, true);

Subscribe and route

Map a topic to an Event, or push it to the queue as a Job.

app/Providers/AppServiceProvider.php
use Haiderkamran\LaravelMqtt\Facades\Mqtt;
use App\Events\TemperatureUpdated;
use App\Jobs\ProcessDeviceData;
 
public function boot()
{
    // Exact Match -> Dispatches a Laravel Event
    Mqtt::topic('sensor/temp')->event(TemperatureUpdated::class);
 
    // Wildcard Match -> Dispatches a Laravel Job to the Queue
    Mqtt::topic('device/#')->dispatch(ProcessDeviceData::class);
 
    // Single-level wildcard support
    Mqtt::topic('room/+/lights')->dispatch(ProcessLightCommand::class);
}

Anatomy of a publish with QoS and retain

  1. Topic where the message goes.
  2. Payload a string, or an array that is JSON-encoded.
  3. QoS level a PHP 8.1 enum, QosLevel; exactly once here.
  4. Retain flag asks the broker to keep this as the topic's last message.
Features

Horizon-like MQTT for Laravel

Pub/Sub architecture · IoT integration · Real-time messaging

  • Event and job dispatching

    Map incoming topics to Laravel Events or Jobs, so heavy processing runs on your queues.

  • Clean facade API

    Publishing and topic routing with Mqtt::publish and Mqtt::topic.

  • Robust worker daemon

    Memory protection, graceful SIGTERM shutdown and a heartbeat lock.

  • Auto reconnect and backoff

    Configurable exponential backoff for broker disconnections.

  • Topic wildcards

    Full support for + and #.

  • Native broadcasting

    A custom mqtt driver for Laravel broadcasting.

  • Real-time dashboard

    Live metrics, available out of the box.

  • Strictly typed

    PHP 8.1 enums for QosLevel and protocols.

Worker daemon

A long-running worker you can trust

php artisan mqtt:work behaves like a Laravel queue worker. Run it under Supervisor in production.

Terminal
# Start listening for the routed topics
php artisan mqtt:work
 
# After a hard crash leaves the lock stuck
php artisan mqtt:clear
  • One worker at a timeLaravel's cache acts as a mutex lock, so a second worker cannot duplicate messages.
  • Graceful shutdownSIGTERM is handled, with built-in memory protection.
  • Heartbeat lock managerPrevents duplicate processes.
  • Reconnect with backoffExponential backoff when the broker drops the connection.
Broadcasting and dashboard

Your events on MQTT, your metrics at /mqtt

A native mqtt broadcast driver

Set the driver and any event that implements ShouldBroadcast publishes its payload to the broker.

.env
BROADCAST_DRIVER=mqtt
private-user.1private-user/1

Laravel dot-notation channels become MQTT slash-notation topics automatically.

Messages received12,480
Messages published1,236
Connection drops2
Worker daemonRunningmqtt:work
Illustrative dashboard with sample numbers
Install

Composer, publish, connect

Laravel MQTT is maintained in the open, versioned semantically, and used in our own client projects — so fixes land upstream rather than in a private fork.

Terminal
composer require haider-kamran/laravel-mqtt
 
php artisan vendor:publish --tag=mqtt-config
php artisan vendor:publish --tag=mqtt-views
.env
MQTT_HOST=127.0.0.1
MQTT_PORT=1883
MQTT_CLIENT_ID=laravel-app
MQTT_USERNAME=
MQTT_PASSWORD=
MQTT_PROTOCOL=3.1.1
MQTT_KEEP_ALIVE=60
MQTT_RECONNECT=true
MQTT_RECONNECT_MAX_DELAY=60

From the package README on GitHub

Packagehaider-kamran/laravel-mqtt
LicenceMIT
LanguagePHP / Laravel
RequiresPHP 8.1 or laterLaravel 9 or later
Maintained byApexlyTech, in the open on GitHub
View on GitHub

Connect your devices to Laravel

A high-performance MQTT client implementation for Laravel, allowing robust publish/subscribe messaging for IoT devices and real-time backend communication.