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?

anonymous2
  • 4,902
  • 3
  • 22
  • 49

2 Answers2

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.

hardillb
  • 12,813
  • 1
  • 21
  • 34
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.

DavidJ
  • 11
  • 1