Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check confirmation - QoS 1 #8

Closed
divix1988 opened this issue Jul 10, 2024 · 3 comments
Closed

Check confirmation - QoS 1 #8

divix1988 opened this issue Jul 10, 2024 · 3 comments

Comments

@divix1988
Copy link

I am publishing a message with:

$client->publish('foo/bar/baz', 'Hello world!', MqttClient::QOS_AT_LEAST_ONCE);

How do I check if the message was confirmed and PUBACK was sent back?

@divix1988 divix1988 changed the title Check confirmation Check confirmation - QoS 1 Jul 10, 2024
@Namoshek
Copy link
Collaborator

You can't really check that specifically, but the client will attempt to resend the message in case no PUBACK is received based on the configured timeout in the ConnectionSettings.

However, for QoS > 0 you need to run $client->loop() after publishing the message. You can use the second and third parameter to tune your publishing:

$client->loop(allowSleep: true, exitWhenQueuesEmpty: true, queueWaitLimit: 10);

This will exit the loop immediately when PUBACK is received, but it will retry up to 10 seconds. If the message could not be delivered within 10 seconds, it will exit anyway. See MqttClient::loop() for details.

A more complete example would be:

$connectionSettings = (new ConnectionSettings())
    ->setResendTimeout(3); // Resend messages every 3 seconds

$client = new MqttClient($server, $port, $clientId);
$client->connect($connectionSettings);

$client->publish('foo/bar/baz', 'Hello world!', MqttClient::QOS_AT_LEAST_ONCE);
$client->loop(allowSleep: true, exitWhenQueuesEmpty: true, queueWaitLimit: 10);

This will attempt to resend the message every 3 seconds for up to 10 seconds (i.e. 4 attempts).

You can also omit the queueWaitLimit, then the client will retry forever. That, however, can cause deadlocks as you might guess.

@divix1988
Copy link
Author

divix1988 commented Jul 11, 2024

Thanks for reply.

Right, what I have to do is to display a message saying: data is syncing with broker to the end user when I publish a new message and when PUBACK is confirmed then I need to swap the message to data has been synced successfully

Is there a way to check if PUBACK was confirmed or at least that an error has ocurred? just so I can inject my logic into there?

I am currently using: $client->registerMessageReceivedEventHandler($handler); and look for same $topic to be returned. Would this even work?

@Namoshek
Copy link
Collaborator

There is no way to check the transmission result or status of individual messages. So the only way you can know for sure that a queued message (or all queued messages) was successfully sent to the broker is by looping until the loop exits by itself:

$client->loop(allowSleep: true);

However, you should note that this only tells you whether the message was sent to the broker successfully, but not if any of the subscribers actually received the message. If you, for example, want to push a command to an individual client indirectly via an MQTT broker, the client should acknowledge the command on a separate topic. It's called the RPC pattern.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants