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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-mqtt

Laravel MQTT

Latest Version on Packagist Total Downloads Tests

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

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_id and $retain are 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(...) and Mqtt::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:

  1. Bump "version" in composer.json (and update CHANGELOG.md) in your pull request.
  2. Merge the pull request into master.
  3. The release workflow runs the test suite, creates the matching vX.Y.Z tag 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.


All versions of laravel-mqtt with dependencies

PHP Build Version
Package Version
Requires php Version ^7.2 || ^8.0
psr/log Version ^1.0 || ^2.0 || ^3.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package salmanzafar/laravel-mqtt contains the following files

Loading the files please wait ...