Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
git-developer opened this issue May 28, 2024 · 2 comments
Open

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

git-developer opened this issue May 28, 2024 · 2 comments

Comments

@git-developer
Copy link

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

@jbdrucker
Copy link

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.

@git-developer
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants