Publish messages only when active subscribers are present

This commit is contained in:
bergware
2023-11-28 00:56:57 +01:00
parent 469244dacf
commit 7bc581e77a

View File

@@ -10,20 +10,16 @@
*/
?>
<?
function curl_socket($socket, $url, $message) {
$com = curl_init($url);
curl_setopt_array($com,[
CURLOPT_UNIX_SOCKET_PATH => $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 ?? '';
}
?>