-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy path02_subscribe_with_qos_1.php
45 lines (35 loc) · 2.04 KB
/
02_subscribe_with_qos_1.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../shared/config.php';
use PhpMqtt\Client\Examples\Shared\SimpleLogger;
use PhpMqtt\Client\Exceptions\MqttClientException;
use PhpMqtt\Client\MqttClient;
use Psr\Log\LogLevel;
// Create an instance of a PSR-3 compliant logger. For this example, we will also use the logger to log exceptions.
$logger = new SimpleLogger(LogLevel::INFO);
try {
// Create a new instance of an MQTT client and configure it to use the shared broker host and port.
$client = new MqttClient(MQTT_BROKER_HOST, MQTT_BROKER_PORT, 'test-subscriber', MqttClient::MQTT_3_1, null, $logger);
// Connect to the broker without specific connection settings but with a clean session.
$client->connect(null, true);
// Subscribe to the topic 'foo/bar/baz' using QoS 1.
$client->subscribe('foo/bar/baz', function (string $topic, string $message, bool $retained) use ($logger, $client) {
$logger->info('We received a {typeOfMessage} on topic [{topic}]: {message}', [
'topic' => $topic,
'message' => $message,
'typeOfMessage' => $retained ? 'retained message' : 'message',
]);
// After receiving the first message on the subscribed topic, we want the client to stop listening for messages.
$client->interrupt();
}, MqttClient::QOS_AT_LEAST_ONCE);
// Since subscribing requires to wait for messages, we need to start the client loop which takes care of receiving,
// parsing and delivering messages to the registered callbacks. The loop will run indefinitely, until a message
// is received, which will interrupt the loop.
$client->loop(true);
// Gracefully terminate the connection to the broker.
$client->disconnect();
} catch (MqttClientException $e) {
// MqttClientException is the base exception of all exceptions in the library. Catching it will catch all MQTT related exceptions.
$logger->error('Subscribing to a topic using QoS 1 failed. An exception occurred.', ['exception' => $e]);
}