Publish messages only when active subscribers are present

This commit is contained in:
bergware
2023-11-28 04:15:50 +01:00
parent 989bef6130
commit bd0c5c4e09
+7 -5
View File
@@ -1,5 +1,6 @@
<?PHP
/* Copyright 2005-2023, Lime Technology
* Copyright 2012-2023, Bergware International.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License version 2,
@@ -12,14 +13,15 @@
<?
function publish($endpoint, $message, $len=1) {
$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);
curl_setopt_array($com, [CURLOPT_UNIX_SOCKET_PATH => "/var/run/nginx.socket", CURLOPT_HTTPHEADER => ['Accept:text/json'], CURLOPT_RETURNTRANSFER => 1]);
$reply = json_decode(curl_exec($com),true);
// only send message when active subscribers are present
if (!empty($subs[1]) && $subs[1]>0) {
if (($reply['subscribers']??0)>0) {
curl_setopt_array($com, [CURLOPT_POST => 1, CURLOPT_POSTFIELDS => $message]);
$reply = curl_exec($com);
$reply = json_decode(curl_exec($com),true);
}
curl_close($com);
return $reply ?? '';
// return number of active subscribers
return $reply['subscribers']??0;
}
?>