Laravel MQTT
A high-performance MQTT client implementation for Laravel, allowing robust publish/subscribe messaging for IoT devices and real-time backend communication.
Mqtt::publish('device/1/cmd', ['reboot' => true])- 09:41:05room/12/lights{"on":true}
- 09:41:04device/1/data{"battery":87}
- 09:41:03sensor/temp{"celsius":22.4}
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 matchEventMqtt::topic('device/#')Multi-level wildcard, queues ProcessDeviceData · no matchJobMqtt::topic('room/+/lights')Single-level wildcard, queues ProcessLightCommand · matchJob
Publish in one line, subscribe in three
Publish
Arrays are JSON-encoded automatically.
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.
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
- Topic where the message goes.
- Payload a string, or an array that is JSON-encoded.
- QoS level a PHP 8.1 enum,
QosLevel; exactly once here. - Retain flag asks the broker to keep this as the topic's last message.
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.
A long-running worker you can trust
php artisan mqtt:work behaves like a Laravel queue worker. Run it under Supervisor in production.
# 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.
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.
BROADCAST_DRIVER=mqtt
Laravel dot-notation channels become MQTT slash-notation topics automatically.
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.
composer require haider-kamran/laravel-mqtt
php artisan vendor:publish --tag=mqtt-config
php artisan vendor:publish --tag=mqtt-views
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
| Package | haider-kamran/laravel-mqtt |
|---|---|
| Licence | MIT |
| Language | PHP / Laravel |
| Requires | PHP 8.1 or laterLaravel 9 or later |
| Maintained by | ApexlyTech, in the open 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.