Refactor: Support not sending duplicate messages

Keep track of the md5 within a separate function.  Avoids duplication of code on the nchan scripts, and allows for the script to still exit if no listeners without adding in more code within each script to handle this.
This commit is contained in:
Squidly271
2025-08-10 18:13:10 -04:00
parent fa5c87cc75
commit 408331edf5
9 changed files with 43 additions and 28 deletions

View File

@@ -34,7 +34,7 @@ function curl_socket($socket, $url, $message='') {
// $opt3: if $opt1 is not numeric, it's a value for $abortTime.
function publish($endpoint, $message, $opt1=1, $opt2=false, $opt3=120) {
static $abortStart = [], $com = [], $lens = [];
if ( is_file("/tmp/publishPaused") )
return false;
@@ -107,6 +107,36 @@ function publish($endpoint, $message, $opt1=1, $opt2=false, $opt3=120) {
return $reply;
}
// Function to not continually republish the same message if it hasn't changed since the last publish
function publish_md5($endpoint, $message, $opt1=1, $opt2=false, $opt3=120) {
static $md5_old = [];
static $md5_time = [];
if ( is_numeric($opt1) ) {
$timeout = $opt3;
$abort = $opt2;
} else {
$abort = $opt1;
$timeout = $opt2 ?: $opt3;
}
// if abort is set, republish the message even if it hasn't changed after $timeout seconds to check for subscribers and exit accordingly
if ( $abort ) {
if ( (time() - ($md5_time[$endpoint]??0)) > $timeout ) {
$md5_old[$endpoint] = null;
}
}
$md5_new = $message ? md5($message,true) : -1 ;
if ($md5_new !== ($md5_old[$endpoint]??null)) {
$md5_old[$endpoint] = $md5_new;
$md5_time[$endpoint] = time();
return publish($endpoint, $message, $opt1, $opt2, $opt3);
}
}
// Removes the script calling this function from nchan.pid
function removeNChanScript() {
global $docroot, $argv;