feat(home): live updates via Mercure — server pushes device state to the PWA
CI / test (push) Has been cancelled

Subscribe per-device with a Symfony Mercure hub: server publishes a fresh
device payload after every poll (200/304/204), every PATCH, and every
lock/unlock. The frontend opens one EventSource per device topic and
splats inbound JSON straight into the devices store — same shape as
GET /api/devices, so no envelope handling.

Topic: https://pictureframe.edholm.me/devices/{id}

Stack mirrors aqua-iq:
- symfony/mercure-bundle + config/packages/mercure.yaml
- App\Service\MercurePublisher (errors swallowed + logged; a flaky hub
  must not break a poll response)
- App\Service\DeviceSerializer extracted as the single source of truth
  for the wire shape (REST + Mercure share it)
- Frontend useDeviceMercure() composable: opens/closes EventSources to
  match the device list reactively, reconnects on hub-side closes
- SpaController exposes MERCURE_PUBLIC_URL via window.__PF_MERCURE_URL__

Production compose adds a dunglas/mercure container with Traefik labels
for pictureframe.edholm.me/.well-known/mercure (handled separately on
the host since the file isn't in this repo).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-07 16:20:21 -04:00
parent 995445ed9e
commit ba9625d45d
32 changed files with 529 additions and 43 deletions
+36
View File
@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace App\Service;
use App\Entity\Device;
/**
* Single source of truth for the wire shape of a Device. The REST API uses it
* to render `/api/devices` responses; the Mercure publisher uses it so live
* pushes are byte-identical to a fresh GET, letting the SPA splat the payload
* straight into its store.
*/
final class DeviceSerializer
{
/** @return array<string, mixed> */
public function serialize(Device $d): array
{
return [
'id' => $d->getId(),
'mac' => $d->getMac(),
'name' => $d->getName(),
'orientation' => $d->getOrientation()->value,
'rotationIntervalMinutes' => $d->getRotationIntervalMinutes(),
'wakeTimes' => $d->getWakeTimes(),
'timezone' => $d->getTimezone(),
'uniquenessWindow' => $d->getUniquenessWindow(),
'linkedAt' => $d->getLinkedAt()->format(\DateTimeInterface::ATOM),
'lastSeenAt' => $d->getLastSeenAt()?->format(\DateTimeInterface::ATOM),
'nextPollExpectedAt' => $d->getNextPollExpectedAt()?->format(\DateTimeInterface::ATOM),
'lockedImageId' => $d->getLockedImage()?->getId(),
'currentImageId' => $d->getCurrentImage()?->getId(),
];
}
}
+45
View File
@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace App\Service;
use Psr\Log\LoggerInterface;
use Symfony\Component\Mercure\HubInterface;
use Symfony\Component\Mercure\Update;
/**
* Thin wrapper around the Mercure hub for device-state pushes.
*
* Topic shape: `https://pictureframe.edholm.me/devices/{id}` — same convention
* aqua-iq uses. The browser subscribes per device id; the published payload is
* the same JSON the REST API would have returned, so the SPA can splat it
* straight into the device store with no separate envelope handling.
*
* Errors are swallowed and logged — a flaky hub must never break a poll
* response or a settings PATCH for the user.
*/
final class MercurePublisher
{
public const TOPIC_PREFIX = 'https://pictureframe.edholm.me/devices/';
public function __construct(
private readonly HubInterface $hub,
private readonly LoggerInterface $logger,
) {}
public function publishDevice(int $deviceId, array $payload): void
{
try {
$this->hub->publish(new Update(
self::TOPIC_PREFIX . $deviceId,
json_encode($payload, JSON_THROW_ON_ERROR),
));
} catch (\Throwable $e) {
$this->logger->warning('mercure.publish_failed', [
'device_id' => $deviceId,
'error' => $e->getMessage(),
]);
}
}
}