0
votes

In the context on the MQTT protocol, is there a way to make a client not send publish messages when there are no subscribers to that topic?

In other words, is there a standard way to perform subscriber-aware publishing, reducing network traffic from publishing clients to the broker?

This important in applications where we have many sensors capable of producing huge amounts of data, but most of the time nobody will be interested in all of that data but on a small subset, and we want to save battery or avoid network congestion.

3

3 Answers

3
votes

In the upcoming MQTT v5 specification the broker can indicate to a client that there are no subscribers for a topic when the client publishes to that topic. This is only possible for QoS 1 or QoS 2 publishes because a QoS 0 message does not result in a reply.

1
votes

No, the publisher has absolutely no idea how many subscribers to a given topic there are, there could be zero or thousands.

This is a key point to pub/sub messaging, the near total decoupling of the information producer and consumer.

1
votes

Presumably you can design your devices and applications so that the device as well as publishing data to a 'data topic', it also subscribes to another device-specific 'command topic' which controls the device data publishing. If the application is interested in data from a specific device it must know which device that is to know which data topic to subscribe to, so it can publish the 'please publish data now' command to the corresponding command topic.

I suppose there might be a somewhere-in-between solution where devices publish data less frequently when no apps are interested, and faster when at least one app is asking for data to be published.

Seems to me that one thing about MQTT is that you should ideally design the devices and applications as a system, not in isolation.