4

I understand an MQTT client publishes data to a topic after connect message.

I am not sure how an MQTT client subscribing to a topic get updated when there is a publish to that topic.

  • Is it the subscriber that has to initiate a connection and then send a subscribe message each time to check the latest published update?

  • Or does the subscriber send a subscribe message once to the broker, and whenever there is a new publish, the broker automatically sends the published data to the subscriber?

Is there any option to automatically initiate a communication from broker to subscriber client to update the client with latest published data?

2 Answers 2

2

The important thing to remember is that when a client (a client can be a subscriber, publisher or both) connects to the broker the connection is held open for the life of the client. This means that there is always a link between the 2.

When a client subscribes it sends a list of topic (or topic patterns if it includes wildcards) to the broker. When the broker receives a message which matches any entries on that list, it sends the message to that client down the existing connection. It does not need to open a new connection.

2
  • Thanks for the response. But from other sources I understand that broker terminates the connection with client on keep alive timeout. Is it false? Mar 11, 2021 at 8:35
  • Yes it will, but it also sends ping messages to ensure the keep alive timeout doesn't happen unless there is an issue with the client or broker.
    – hardillb
    Mar 11, 2021 at 8:41
0

Your second bullet question is correct.

And when the publisher sends a message with the 'retain' flag set, any subscriber will receive that latest message when it registers with the broker.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.