From 7bc581e77a8d852a1d52b504301f1b2cf2607aff Mon Sep 17 00:00:00 2001 From: bergware Date: Tue, 28 Nov 2023 00:56:57 +0100 Subject: [PATCH] Publish messages only when active subscribers are present --- emhttp/plugins/dynamix/include/publish.php | 24 +++++++++------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/emhttp/plugins/dynamix/include/publish.php b/emhttp/plugins/dynamix/include/publish.php index 9dc5a44ce..198a77f3b 100644 --- a/emhttp/plugins/dynamix/include/publish.php +++ b/emhttp/plugins/dynamix/include/publish.php @@ -10,20 +10,16 @@ */ ?> $socket, - CURLOPT_POST=> 1, - CURLOPT_POSTFIELDS => $message, - CURLOPT_RETURNTRANSFER => true - ]); - $reply = curl_exec($com); - curl_close($com); - return $reply; -} - function publish($endpoint, $message, $len=1) { - return curl_socket("/var/run/nginx.socket", "http://localhost/pub/$endpoint?buffer_length=$len", $message); + $com = curl_init("http://localhost/pub/$endpoint?buffer_length=$len"); + curl_setopt_array($com, [CURLOPT_UNIX_SOCKET_PATH => "/var/run/nginx.socket", CURLOPT_RETURNTRANSFER => true]); + preg_match('/subscribers: (\d+)/', curl_exec($com), $subs); + // only send message when active subscribers are present + if (!empty($subs[1]) && $subs[1]>0) { + curl_setopt_array($com, [CURLOPT_POST => 1, CURLOPT_POSTFIELDS => $message]); + $reply = curl_exec($com); + } + curl_close($com); + return $reply ?? ''; } ?>