mosquitto icon indicating copy to clipboard operation
mosquitto copied to clipboard

Allow topic per message for `mosquitto_pub --stdin-line`

Open git-developer opened this issue 1 year ago • 2 comments

This is a feature request to extend mosquitto_pub --std-line so that it allows a separate topic per message.

Use case

mosquitto_pub can be used as bridge between a program in a linux shell and an MQTT broker. The argument --std-line supports this efficiently because it allows to establish the MQTT connection once for an arbitrary number of messages. Unfortunately this is limited to a single topic for all messages.

There are scenarios where the topic depends on the message. Let's take this simple example (borrowed from a real StackOverflow question) where a program outputs values of different thermometers:

$ temps_radio --format "temp_%n=%v C"
temp_0d=23.5 C
temp_02=11.3 C
temp_04=5.0 C
…

Imagine we'd like to send these values to a broker using a topic per thermometer, e.g. sensors/temp_0d, sensors/temp_02, sensors/temp_04. If we do this with mosquitto_pub, a connection is established for each message. This is very inefficient for a high message count, even more if TLS is used.

This could be improved by establishing a single connection once and configuring the topic per message. Example (fictive syntax):

mosquitto_pub --stdin-line --topic-separator ':' <<EOF
sensors/temp_0d:23.5 C
sensors/temp_02:11.3 C
sensors/temp_04:5.0 C
EOF

The desired effect is that mosquitto_pub establishes a single connection and then sends three messages with the following arguments

  • -t sensors/temp_0d -m '23.5 C'
  • -t sensors/temp_02 -m '11.3 C'
  • -t sensors/temp_04 -m '5.0 C'

The requested behavior might be achieved by using/coding another mqtt client, but I think it's a common use case for users of mosquitto_pub.

References

git-developer avatar May 28 '24 17:05 git-developer

I came here today to search for this exact feature, came across the same StackOverflow article.

My need is to pipe mosquitto_sub to mosquitto_pub to send retained messages from one broker to the other (I don't have access to the mosquitto.db file). For now I need a script with a loop and one mosquitto_pub per message. Feeding the topic through stdin would only require one mosquitto_pub.

jbdrucker avatar May 29 '24 13:05 jbdrucker

To address the use case above, git-developer/pimper can be used as workaround until this feature is implemented. It is a gateway between file system pipes and MQTT.

git-developer avatar Jul 09 '24 04:07 git-developer