Download the PHP package utopia-php/mqtt without Composer
On this page you can find all versions of the php package utopia-php/mqtt. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package mqtt
Utopia MQTT
[!IMPORTANT] This repository is a read-only mirror of
packages/mqttin appwrite/appwrite. Development happens there — please open issues and pull requests against appwrite/appwrite.
Utopia MQTT is a PHP toolkit for building MQTT brokers (3.1.1 and 5.0). You implement one interface of typed control packets and the library owns the wire — framing, decoding, per-version encoding, packet ids, the QoS handshake, keep-alive reaping, and subscription matching. This library is maintained by the Appwrite team and is framework-agnostic.
Installation
The library requires PHP 8.2+. The Swoole adapter additionally needs the Swoole extension.
Quick start
Create a broker by wiring an adapter (which transports to listen on) with a handler (how packets are answered). The handler below accepts every client, grants every subscription, and fans each publish out to its subscribers.
The broker listens on TCP port 1883 and speaks both 3.1.1 and 5.0. Implement Handler to add authentication, per-topic authorization, offline delivery, or any policy your broker needs.
Handlers
The Handler is the whole application surface. Each method receives a decoded control packet and returns a decision — itself a packet, which the broker encodes for the client's negotiated version. You never touch bytes, packet ids, or version encoders.
Authenticate a CONNECT by returning a Connack (v5 enhanced auth may return an Auth challenge instead):
Authorize each subscribed filter, granting a QoS or denying it:
Delivery is yours. onPublish receives the local subscribers the broker matched; publish to each Connection, and acknowledge the sender's QoS 1 message once you have accepted it:
Because the handler is a single narrow interface, cross-cutting policy composes as decorators that wrap another handler:
The packet each method works with:
| Received | Returned |
|---|---|
Connect — clientId, cleanStart, keepAlive, username, password, will, v5 authMethod/authData, userProperties() |
Connack::accept() / Connack::refuse($reason) |
Subscribe — filters() of Filter { topic, qos } |
Suback — grant($qos) / deny() per filter |
Unsubscribe — filters() |
Unsuback — success() / fail() per filter |
Publish — topic, payload, qos, dup, retain, packetId, userProperties() |
delivered, not replied |
Auth — method, data, userProperties() |
Auth::success() / Auth::challenge($method, $data) |
Puback — packetId |
resolve with $connection->acknowledge($packetId) |
Connections
The Connection handed to every method is the per-client handle. It carries transport state (protocol, cleanStart, keepAlive), the isolation key subscriptions match under (prefix), and an opaque identity the library never reads. You publish through it:
For QoS 1 it tracks in-flight deliveries, so a PUBACK resolves back to the topic and durable sequence it acknowledges (track / resume / acknowledge), advancing a cursor only across a contiguous run of acks — the foundation for offline replay on reconnect.
Adapters and transports
An adapter composes one or more transports into a single process. The Swoole adapter ships Tcp, Tls, and WebSocket transports; UDP-style browsers over WebSocket and native clients over TCP feed the same handler.
A WebSocket message may carry several or partial MQTT packets, so the adapter reassembles whole packets before dispatch and pushes binary frames on send. Keep-alive reaping is intrinsic to MQTT and runs inside the adapter: every inbound packet re-arms a connection's deadline, and a client silent past keepAlive × 1.5 is closed, surfacing as onDisconnect(null, ...).
Telemetry
Pass a utopia-php/telemetry adapter to record broker metrics — packets received by type, connections opened and active, and subscriptions granted or denied. It defaults to a no-op adapter.
Application-specific metrics (authentication latency, delivery counts, and so on) stay in your handler.
MQTT client
The bundled Client talks to a broker over TCP/TLS (mqtt:// / mqtts://): connect, send, receive / listen, with onOpen / onReceive / onClose / onError.
Tests
The unit tier needs no services. The e2e tier starts the Swoole fixture broker in tests/Fixtures/Swoole on ports 18830 (TCP) and 18831 (WebSocket) and runs against it.
Copyright and license
The MIT License (MIT) http://www.opensource.org/licenses/mit-license.php