Send timestamp along with individual SSE NP events, avoiding separate `global:time` channel.

This commit is contained in:
Buster Neece 2024-03-19 05:27:42 -05:00
parent 5212e54ad6
commit 4324f94cb2
No known key found for this signature in database
6 changed files with 21 additions and 87 deletions

View File

@ -129,7 +129,6 @@ return static function (CallableEventDispatcherInterface $dispatcher) {
App\Sync\Task\RotateLogsTask::class,
App\Sync\Task\RunAnalyticsTask::class,
App\Sync\Task\RunBackupTask::class,
App\Sync\Task\SendTimeOnSocketTask::class,
App\Sync\Task\UpdateGeoLiteTask::class,
App\Sync\Task\UpdateStorageLocationSizesTask::class,
]);

View File

@ -72,7 +72,6 @@ export default function useNowPlaying(props) {
"cf_connect": JSON.stringify({
"subs": {
[`station:${props.stationShortName}`]: {},
"global:time": {},
}
}),
});
@ -80,19 +79,20 @@ export default function useNowPlaying(props) {
const handleSseData = (ssePayload) => {
const jsonData = ssePayload?.pub?.data ?? {};
if (ssePayload.channel === 'global:time') {
currentTime.value = jsonData.time;
if ('current_time' in jsonData) {
currentTime.value = jsonData.current_time;
}
if (npTimestamp.value === 0) {
setNowPlaying(jsonData.np);
} else {
if (npTimestamp.value === 0) {
// SSE events often dispatch *too quickly* relative to the delays involved in
// Liquidsoap and Icecast, so we delay these changes from showing up to better
// approximate when listeners will really hear the track change.
setTimeout(() => {
setNowPlaying(jsonData.np);
} else {
// SSE events often dispatch *too quickly* relative to the delays involved in
// Liquidsoap and Icecast, so we delay these changes from showing up to better
// approximate when listeners will really hear the track change.
setTimeout(() => {
setNowPlaying(jsonData.np);
}, 3000);
}
}, 3000);
}
}

View File

@ -12,8 +12,6 @@ final class Centrifugo
{
use EnvironmentAwareTrait;
public const GLOBAL_TIME_CHANNEL = 'global:time';
public function __construct(
private readonly Client $client,
) {
@ -24,24 +22,6 @@ final class Centrifugo
return $this->environment->isDocker() && !$this->environment->isTesting();
}
public function sendTime(): void
{
$this->send([
'method' => 'publish',
'params' => [
'channel' => self::GLOBAL_TIME_CHANNEL,
'data' => $this->buildTimeMessage(),
],
]);
}
public function buildTimeMessage(): array
{
return [
'time' => time(),
];
}
public function publishToStation(Station $station, mixed $message, array $triggers): void
{
$this->send([
@ -58,6 +38,7 @@ final class Centrifugo
return [
'np' => $message,
'triggers' => $triggers,
'current_time' => time(),
];
}

View File

@ -61,7 +61,7 @@ final class EventHandler
{
$channels = array_filter(
$request->channels,
fn($channel) => str_starts_with($channel, 'station:') || $channel === Centrifugo::GLOBAL_TIME_CHANNEL
fn($channel) => str_starts_with($channel, 'station:')
);
if (empty($channels)) {
@ -71,27 +71,19 @@ final class EventHandler
$allInitialData = [];
foreach ($channels as $channel) {
if ($channel === Centrifugo::GLOBAL_TIME_CHANNEL) {
$initialData = $this->centrifugo->buildTimeMessage();
} elseif (str_starts_with($channel, 'station:')) {
$stationName = substr($channel, 8);
$np = $this->npCache->getForStation($stationName);
if (!($np instanceof NowPlaying)) {
continue;
}
$np->resolveUrls($this->router->getBaseUrl());
$np->update();
$initialData = $this->centrifugo->buildStationMessage($np);
} else {
$stationName = substr($channel, 8);
$np = $this->npCache->getForStation($stationName);
if (!($np instanceof NowPlaying)) {
continue;
}
$np->resolveUrls($this->router->getBaseUrl());
$np->update();
$allInitialData[] = [
'channel' => $channel,
'pub' => [
'data' => $initialData,
'data' => $this->centrifugo->buildStationMessage($np),
],
];
}

View File

@ -1,29 +0,0 @@
<?php
declare(strict_types=1);
namespace App\Sync\Task;
use App\Service\Centrifugo;
final class SendTimeOnSocketTask extends AbstractTask
{
public function __construct(
private readonly Centrifugo $centrifugo,
) {
}
public static function getSchedulePattern(): string
{
return self::SCHEDULE_EVERY_MINUTE;
}
public function run(bool $force = false): void
{
if (!$this->centrifugo->isSupported()) {
return;
}
$this->centrifugo->sendTime();
}
}

View File

@ -28,12 +28,3 @@ allow_subscribe_for_client = true
allow_subscribe_for_anonymous = true
allow_history_for_client = true
allow_history_for_anonymous = true
[[namespaces]]
name = "global"
history_size = 1
history_ttl = "2m"
allow_subscribe_for_client = true
allow_subscribe_for_anonymous = true
allow_history_for_client = true
allow_history_for_anonymous = true