]> git.ipfire.org Git - thirdparty/shairport-sync.git/commitdiff
Add MQTT retain flag support 2142/head
authorlululombard <lululombard@gmail.com>
Sun, 8 Feb 2026 11:43:53 +0000 (12:43 +0100)
committerlululombard <lululombard@gmail.com>
Sun, 8 Feb 2026 11:43:53 +0000 (12:43 +0100)
MQTT.md
common.h
mqtt.c
scripts/shairport-sync.conf
shairport.c

diff --git a/MQTT.md b/MQTT.md
index 5b63e397371c4b5909c827e7d40b8fb9495c9885..a4146f45935521f6ed055c50e2a593be84b7d112 100644 (file)
--- a/MQTT.md
+++ b/MQTT.md
@@ -48,6 +48,7 @@ mqtt =
 //     publish_raw = "no"; // Whether to publish all available metadata under the codes given in the 'metadata' docs.
        publish_parsed = "yes"; // Whether to publish a small (but useful) subset of metadata under human-understandable topics.
        publish_cover = "yes"; // Whether to publish the cover over MQTT in binary form. This may lead to a bit of load on the broker.
+//     publish_retain = "no"; // Whether to set the retain flag on published MQTT messages. When enabled, the broker stores the last message for each topic so new subscribers receive the most recent value immediately.
 //     enable_remote = "no"; // Whether to remote control via MQTT. RC is available under `topic`/remote.
 };
 ```
index b887dbe2bb70aa5589b777aa56a0e38f82f8233c..bf47282911f852c685ffea7325ece1646c79dcb3 100644 (file)
--- a/common.h
+++ b/common.h
@@ -230,6 +230,7 @@ typedef struct {
   int mqtt_publish_raw;
   int mqtt_publish_parsed;
   int mqtt_publish_cover;
+  int mqtt_publish_retain;
   int mqtt_enable_remote;
   int mqtt_enable_autodiscovery;
   char *mqtt_autodiscovery_prefix;
diff --git a/mqtt.c b/mqtt.c
index bcabcccebe02c37417d5fd1af5a6f7c76c4f0461..14178fd3042bf8cc53aceec1c38877ee0af62437 100644 (file)
--- a/mqtt.c
+++ b/mqtt.c
@@ -243,7 +243,8 @@ void mqtt_publish(char *topic, char *data_in, uint32_t length_in) {
   debug(2, "[MQTT]: publishing under %s", fulltopic);
 
   int rc;
-  if ((rc = mosquitto_publish(global_mosq, NULL, fulltopic, length, data, 0, 0)) !=
+  if ((rc = mosquitto_publish(global_mosq, NULL, fulltopic, length, data, 0,
+                              config.mqtt_publish_retain)) !=
       MOSQ_ERR_SUCCESS) {
     switch (rc) {
     case MOSQ_ERR_NO_CONN:
index 8d2e2ad2ee2e3378bed3d60eb511aba7424a9ae2..88ee0e7225c9c0fae1db4cb1bf2893d89e507f0a 100644 (file)
@@ -364,6 +364,7 @@ mqtt =
 //     Currently published topics:artist,album,title,genre,format,songalbum,volume,client_ip,
 //     Additionally, messages at the topics play_start,play_end,play_flush,play_resume are published
 //     publish_cover = "no"; //whether to publish the cover over mqtt in binary form. This may lead to a bit of load on the broker
+//     publish_retain = "no"; //whether to set the retain flag on published MQTT messages. When enabled, the broker stores the last message for each topic.
 //     enable_autodiscovery = "no"; //whether to publish an autodiscovery message to automatically appear in Home Assistant
 //     autodiscovery_prefix = "homeassistant"; //string to prepend to autodiscovery topic
 //     enable_remote = "no"; //whether to remote control via MQTT. RC is available under `topic`/remote.
index ed2bf07a2292252bddd22e82d3d783ab7e7afaac..f5b061d3f352735473a355f8a1fb464e30739ee2 100644 (file)
@@ -1440,6 +1440,7 @@ int parse_options(int argc, char **argv) {
     config_set_lookup_bool(config.cfg, "mqtt.publish_raw", &config.mqtt_publish_raw);
     config_set_lookup_bool(config.cfg, "mqtt.publish_parsed", &config.mqtt_publish_parsed);
     config_set_lookup_bool(config.cfg, "mqtt.publish_cover", &config.mqtt_publish_cover);
+    config_set_lookup_bool(config.cfg, "mqtt.publish_retain", &config.mqtt_publish_retain);
     if (config.mqtt_publish_cover && !config.get_coverart) {
       die("You need to have metadata.include_cover_art enabled in order to use mqtt.publish_cover");
     }
@@ -3131,6 +3132,8 @@ int main(int argc, char **argv) {
         config.mqtt_publish_parsed ? "" : " not");
   debug(option_print_level, "mqtt will%s publish cover Art.",
         config.mqtt_publish_cover ? "" : " not");
+  debug(option_print_level, "mqtt will%s set retain flag.",
+        config.mqtt_publish_retain ? "" : " not");
   debug(option_print_level, "mqtt remote control is %sabled.",
         config.mqtt_enable_remote ? "en" : "dis");
   debug(option_print_level, "mqtt autodiscovery is %sabled.",