MQTT 2
MQTT 2
php-mqtt/client was created by, and is maintained by Marvin Mall. It allows you to connect to
an MQTT broker where you can publish messages and subscribe to topics. The current
implementation supports all QoS levels (with limitations).
Installation
The package is available on packagist.org and can be installed using composer:
Usage
In the following, only a few very basic examples are given. For more elaborate examples, have a
look at the php-mqtt/client-examples repository.
Publish
A very basic publish example using QoS 0 requires only three steps: connect, publish and
disconnect
$server = 'some-broker.example.com';
$port = 1883;
$clientId = 'test-publisher';
Be also aware that most of the methods can throw exceptions. The above example does not add
any exception handling for brevity.
Subscribe
Subscribing is a little more complex than publishing as it requires to run an event loop which
reads, parses and handles messages from the broker:
$server = 'some-broker.example.com';
$port = 1883;
$clientId = 'test-subscriber';
pcntl_async_signals(true);
$clientId = 'test-subscriber';
Client Settings
As shown in the examples above, the MqttClient takes the server, port and client id as first,
second and third parameter. As fourth parameter, the protocol level can be passed. Currently
supported is MQTT v3.1, available as constant MqttClient::MQTT_3_1. A fifth parameter
allows passing a repository (currently, only a MemoryRepository is available by default). Lastly,
a logger can be passed as sixth parameter. If none is given, a null logger is used instead.
Example:
1. A ConnectionSettings instance
2. A boolean flag indicating whether a clean session should be requested (a random client
id does this implicitly)
Example:
$mqtt->connect($connectionSettings, true);
The ConnectionSettings class provides a few settings through a fluent interface. The type
itself is immutable, and a new ConnectionSettings instance will be created for each added
option. This also prevents changes to the connection settings after a connection has been
established.
// The connect timeout defines the maximum amount of seconds the client
will try to establish
// a socket connection with the broker. The value cannot be less than 1
second.
->setConnectTimeout(60)
// The socket timeout is the maximum amount of idle time in seconds for
the socket connection.
// If no data is read or sent for the given amount of seconds, the socket
will be closed.
// The value cannot be less than 1 second.
->setSocketTimeout(5)
// The resend timeout is the number of seconds the client will wait before
sending a duplicate
// of pending messages without acknowledgement. The value cannot be less
than 1 second.
->setResendTimeout(10)
// Defines the maximum number of reconnect attempts until the client gives
up.
// This setting is only relevant if setReconnectAutomatically() is set to
true.
->setMaxReconnectAttempts(3)
// The keep alive interval is the number of seconds the client will wait
without sending a message
// until it sends a keep alive signal (ping) to the broker. The value
cannot be less than 1 second
// and may not be higher than 65535 seconds. A reasonable value is 10
seconds (the default).
->setKeepAliveInterval(10)
// If the broker should publish a last will message in the name of the
client when the client
// disconnects abruptly, this setting defines the topic on which the
message will be published.
//
// A last will message will only be published if both this setting as well
as the last will
// message are configured.
->setLastWillTopic(null)
// If the broker should publish a last will message in the name of the
client when the client
// disconnects abruptly, this setting defines the message which will be
published.
//
// A last will message will only be published if both this setting as well
as the last will
// topic are configured.
->setLastWillMessage(null)
// The quality of service level the last will message of the client will
be published with,
// if it gets triggered.
->setLastWillQualityOfService(0)
// This flag determines if the last will message of the client will be
retained, if it gets
// triggered. Using this setting can be handy to signal that a client is
offline by publishing
// a retained offline state in the last will and an online state as first
message on connect.
->setRetainLastWill(false)
// This flag determines if TLS should be used for the connection. The port
which is used to
// connect to the broker must support TLS connections.
->setUseTls(false)
Features
Supported MQTT Versions
o v3.1
o v3.1.1
o v5.0
Transport
o TCP (unsecured)