Download the PHP package salmanzafar/laravel-mqtt without Composer
On this page you can find all versions of the php package salmanzafar/laravel-mqtt. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download salmanzafar/laravel-mqtt
More information about salmanzafar/laravel-mqtt
Files in salmanzafar/laravel-mqtt
Package laravel-mqtt
Short Description A simple Laravel Library to connect/publish/subscribe to MQTT broker
License MIT
Informations about the package laravel-mqtt
Laravel MQTT
A simple Laravel library to connect, publish and subscribe to an MQTT broker.
Based on bluerhinos/phpMQTT. A working example application is available in the Laravel-Mqtt-Example repo.
Table of contents
- Compatibility
- Installation
- Configuration
- Usage
- Publishing
- Subscribing
- Multiple topics
- Helper functions
- Artisan commands
- Multiple connections
- Events
- Notifications
- TLS / SSL
- Error handling
- Testing your app
- Available methods
- Testing & quality
- Roadmap
- Releasing
- Changelog
- Contributing
- License
Compatibility
| PHP | Laravel |
|---|---|
| 7.2 – 8.4 | 5.5 – 12.x |
The package uses Laravel's package auto-discovery, so it works out of the box across all of the above versions.
Installation
The service provider and Mqtt facade are registered automatically on
Laravel 5.5+. Only if you are on Laravel < 5.5, register them manually in
config/app.php:
Configuration
Publish the configuration file:
This creates config/mqtt.php:
| Key | Env var | Default | Description |
|---|---|---|---|
host |
MQTT_HOST |
127.0.0.1 |
Broker host. |
port |
MQTT_PORT |
1883 |
Broker port (8883 for TLS). |
username |
MQTT_USERNAME |
'' |
Username, if the broker requires authentication. |
password |
MQTT_PASSWORD |
'' |
Password, if the broker requires authentication. |
timeout |
MQTT_TIMEOUT |
10 |
Connection timeout in seconds. |
keepalive |
MQTT_KEEPALIVE |
10 |
Seconds between keep-alive pings. |
debug |
MQTT_DEBUG |
false |
Enable debug logging. |
qos |
MQTT_QOS |
0 |
Quality of Service level. |
retain |
MQTT_RETAIN |
0 |
Retain flag (0 or 1). |
exceptions |
MQTT_EXCEPTIONS |
false |
Throw on failure instead of returning false. |
tls |
MQTT_TLS_* |
see above | SSL stream-context options used when a CA file is set. |
When running inside Laravel, debug and error messages are written through the framework logger.
Usage
Publishing
Using the class directly:
Using the facade:
$client_idand$retainare optional:ConnectAndPublish($topic, $message)works too. When no client id is given, a unique one is generated for you.
Subscribing
Or via the facade:
Subscribing blocks the process while it listens, so run it from an Artisan command / queue worker rather than an HTTP request.
Subscribing to multiple topics
Pass an array of topics to listen to several at once:
Helper functions
Two convenience helpers are also available:
Artisan commands
Publish or subscribe straight from the CLI — no need to hand-write a console command:
Multiple connections
Talk to more than one broker by defining extra connections in config/mqtt.php.
The top-level settings are the default connection; each named connection
inherits them and overrides only what it needs:
Events
Every received message dispatches a Salman\Mqtt\Events\MqttMessageReceived
event (in addition to your subscription callback), so you can handle messages
with a listener:
Notifications
Send Laravel notifications over MQTT. Add 'mqtt' to via() and return a
payload (or an MqttMessage) from toMqtt():
The topic can come from the MqttMessage or from a
routeNotificationForMqtt() method on the notifiable.
TLS / SSL
Provide a CA file (and optionally a client certificate) to connect over
tls://. Set the broker port to 8883 and configure the certificate paths:
Fine-tune verification through the mqtt.tls options (for example set
MQTT_TLS_ALLOW_SELF_SIGNED=true for a self-signed broker in development). Keep
the verify_peer options enabled in production.
Error handling
By default the connection methods return false on failure. If you prefer
exceptions, enable them:
Testing your app
Use Mqtt::fake() to swap the real client for a recorder and assert on what
your application published — no broker required:
Available methods
| Method | Returns | Description |
|---|---|---|
ConnectAndPublish(string $topic, string $message, string\|int $clientId = null, int $retain = null) |
bool |
Connect, publish a message and disconnect. |
ConnectAndSubscribe(string\|array $topic, callable $callback, string\|int $clientId = null) |
bool |
Connect and listen for messages on one or more topics. |
connection(string $name = null) |
Mqtt |
Get a specific broker connection. |
fake() |
MqttFake |
Swap in a test double that records published messages. |
PHP method names are case-insensitive, so
Mqtt::connectAndPublish(...)andMqtt::connectAndSubscribe(...)work as well.
Testing & quality
Code style (Laravel Pint) and static analysis (PHPStan) are also available:
The Pint and PHPStan binaries are installed on demand by the quality CI
workflow; to run them locally add them once with
composer require --dev laravel/pint larastan/larastan.
Roadmap
The wire protocol is still the legacy phpMQTT 3.1 implementation. The next major
version (v4) will move it onto php-mqtt/client
for MQTT 3.1.1 / 5.0, real QoS 1/2, Last Will & Testament and
message expiry, while keeping the Laravel-facing API. See the
v4 migration plan.
Releasing
Releases are published to
Packagist
automatically. Packagist is connected to this repository via the Packagist
GitHub App, and a GitHub Actions workflow tags releases from the version
field in composer.json:
- Bump
"version"incomposer.json(and updateCHANGELOG.md) in your pull request. - Merge the pull request into
master. - The
releaseworkflow runs the test suite, creates the matchingvX.Y.Ztag and GitHub Release, and Packagist publishes the new version.
Changelog
See CHANGELOG.md for a list of changes.
Contributing
Contributions are welcome — please read CONTRIBUTING.md first.
License
The MIT License (MIT). See LICENSE for details.