mosquitto icon indicating copy to clipboard operation
mosquitto copied to clipboard

JSON with binary data

Open nagy opened this issue 4 years ago • 2 comments

In mosquitto_sub, I would like to receive binary data, while also getting the other data in a json format. When I try to receive binary data now, I get an empty string like so:

echo -ne "\x00\xff\x00" | mosquitto_pub --topic hello --stdin-file
$ mosquitto_sub --topic hello -F %j
{"tst":"2021-05-20T11:45:37.807793Z+0200","topic":"hello","qos":0,"retain":0,"payloadlen":3,"payload":""}

Would it make sense to let the payload, in case that it cannot be decoded into a string, be an array of unsigned integers like [0,255,0]? This format is already adopted by systemds journalctl. See here:

  • https://www.freedesktop.org/software/systemd/man/journalctl.html#-o
  • https://www.freedesktop.org/wiki/Software/systemd/json/

If you think this change makes sense, I can give its implementation a try.

nagy avatar May 20 '21 21:05 nagy

Yes ok, that sounds like a sensible suggestion please have a go :)

ralight avatar May 20 '21 22:05 ralight

Looks like the PR never went through but I think if it ever does, there should be an option to preserve the old behavior of empty payload on binary data as the printed messages get hefty in size.

mateuszdrab avatar Oct 06 '25 22:10 mateuszdrab