eclipse / mosquitto

Eclipse Mosquitto - An open source MQTT broker
https://mosquitto.org
Other
8.61k stars 2.33k forks source link

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

Open git-developer opened 1 month ago

git-developer commented 1 month ago

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

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

jbdrucker commented 1 month ago

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.