Git Product home page Git Product logo

Comments (3)

Namoshek avatar Namoshek commented on July 29, 2024

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.

from client-examples.

divix1988 avatar divix1988 commented on July 29, 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?

from client-examples.

Namoshek avatar Namoshek commented on July 29, 2024

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.

from client-examples.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.