Skip to content

Replace SSE live-status stream with WebSocket (AFD does not relay SSE) - #1942

Merged
David Pine (IEvangelist) merged 2 commits into
mainfrom
ievangelist-live-status-websocket
Oct 23, 2026
Merged

David Pine (IEvangelist) merged 2 commits into
mainfrom
ievangelist-live-status-websocket

Conversation

@IEvangelist

Copy link
Copy Markdown
Member

Summary

Replaces the Server-Sent Events (SSE) live-status stream with a WebSocket stream, because Azure Front Door (AFD) does not relay SSE.

Problem

The first-party doc says it outright: "Front Door doesn't support Server-Sent Events (SSE)" (https://learn.microsoft.com/en-us/azure/frontdoor/standard-premium/websocket). We confirmed it empirically for GET /api/live/stream/:

Path Result
Origin (App Service) 200 text/event-stream, event: state at 0.6 s, :hb comment every 15 s, Cache-Control: no-store, X-Accel-Buffering: no
Through AFD (https://aspire.dev) 0 headers and 0 bytes, then 504 gateway timeout at ~220 s
Browser through AFD new EventSource('/api/live/stream/') never fires open

The client seeded once from GET /api/live/ and then relied only on EventSource, so the live badge went stale in production.

What changed

  • Server (src/statichost/StaticHost)
    • New GET /api/live/ws endpoint (app.UseWebSockets(), KeepAliveInterval 30 s). It reuses the existing LiveStatusBroadcaster / Redis fan-out; only the transport changed.
    • JSON frames: {"type":"state","data":{...}} (same data as the old state event) and {"type":"hb"} every 15 s.
    • Server closes after 80-100% of StreamMaxLifetimeSeconds (default 600 s, key name unchanged) with 1000, and with 1001 on shutdown. Client disconnects and cancellation are handled.
    • Non-WebSocket requests to /api/live/ws get 400.
    • The SSE endpoint /api/live/stream is removed (now 404). /api/live snapshot is unchanged. Telemetry exclusion and the AppHost dashboard URL now point at /api/live/ws.
  • Client (live-status.ts)
    • EventSource replaced by WebSocket (ws:/wss: from location, path /api/live/ws, no trailing slash).
    • Still seeds from /api/live/; exponential backoff with jitter; visibility/online handling kept; DOM/CSS unchanged.
    • Polling fallback: /api/live/ is polled every 30 s whenever the socket is not open (and the tab is visible), so the badge cannot go stale if the socket is blocked. A 45 s stale-socket watchdog closes and reconnects a socket that goes silent.
    • Vite dev proxy for /api/live gets ws: true.
  • AppHost: website.SiteConfig.IsWebSocketsEnabled = true. It was NOT set in the public AppHost, and WebSockets are off by default on App Service, so this is required for the endpoint to work. Please review this setting.
  • Tests/docs: SSE tests replaced by LiveWebSocketTests; vitest and Playwright specs updated; Live/README.md updated.

AFD constraints considered

  • Caching must be disabled on WebSocket routes or AFD drops the Upgrade header. The AFD route has no cache config after Remove Front Door route cache and make /healthz no-store #1937, so this is fine.
  • Idle timeout is 5 min: the 15 s heartbeat (plus 30 s protocol keep-alive) keeps it alive.
  • Connections are closed after at most 2 h and may drop on maintenance: the client reconnects with backoff and the polling fallback covers gaps.
  • Capacity: 3,000 concurrent connections per AFD profile. The ~10 min jittered server lifetime makes clients cycle.
  • WAF only inspects the handshake. AFD health probes use HTTP and are unaffected.

Deploy note

The internal Azure DevOps deploy branch pulls from public main, so it needs to be synced for this to ship (including the App Service WebSockets setting).

Third-party links and affiliations

None. The only new link is the first-party Microsoft Learn AFD WebSocket doc.

Validation

  • dotnet build tests\StaticHost.Tests\StaticHost.Tests.csproj - passed (0 errors; only the existing NU1902 MessagePack warning)
  • dotnet test tests\StaticHost.Tests\StaticHost.Tests.csproj --no-build --filter "FullyQualifiedName!~Live.Redis" - passed, 337/337 (includes new WebSocket tests: initial state, updates, heartbeat, non-WS 400, max-lifetime close, shutdown close, client close/abort)
  • pnpm exec vitest run --config vitest.config.ts tests/unit/live-status.vitest.test.ts tests/unit/astro-vite-config.vitest.test.ts (in src/frontend) - passed, 22/22
  • Local round trip: ran StaticHost on http://127.0.0.1:5099 against a throwaway Redis container; a Node ws client received {"type":"state",...} at 0.2 s and {"type":"hb"} at 15.2 s and 30.2 s, then a clean 1000 close; plain GET /api/live/ws returned 400 and /api/live/stream returned 404.

Not verified

  • Through Front Door: production AFD fronts the currently deployed site, which has no WS endpoint until this ships.
  • Live.Redis* integration tests (need Aspire + Docker containers) were not run.
  • Playwright e2e specs were updated but not run.

Post-deploy checklist

  • App Service webSocketsEnabled is true after deploy
  • Script opens wss://aspire.dev/api/live/ws and shows state + hb messages for 60+ s, including past 220 s (and ideally past 5 min)
  • Live badge updates in a browser on https://aspire.dev with the network tab showing a 101 upgrade

Azure Front Door does not relay Server-Sent Events (documented), so the
live badge went stale in production. Serve live state over /api/live/ws
(JSON frames, 15 s heartbeat, jittered max lifetime), reuse the existing
broadcaster, enable WebSockets on the App Service, and have the client
fall back to polling /api/live/ the whole time the socket is not open.

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 90ef1a20-cd65-43f3-97cd-6fa484d8b0a4
@IEvangelist
David Pine (IEvangelist) marked this pull request as ready for review October 22, 2026 20:33
Copilot AI balanced review requested due to automatic review settings October 22, 2026 20:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The Front Door-to-App Service transport remains unverified, browser integration tests were not run, and a fallback-refresh issue remains unresolved.

1 open issue
What changed in this PR

Replaces the live-status SSE transport with WebSockets so updates can pass through Azure Front Door, while preserving the existing Redis-backed broadcaster and snapshot API.

Changes:

  • Adds WebSocket streaming with heartbeats, bounded connection lifetimes, and shutdown handling.
  • Updates the browser client with reconnects, fallback polling, and a stale-socket watchdog.
  • Enables App Service and development-proxy WebSocket support and updates tests and documentation.
File Description
tests/​StaticHost.Tests/​Live/​LiveWebSocketTests.cs Adds WebSocket lifecycle and message tests.
tests/​StaticHost.Tests/​Live/​LiveStreamTests.cs Removes obsolete SSE tests.
tests/​StaticHost.Tests/​Live/​LiveStatusBroadcasterTests.cs Checks the JSON message envelope.
tests/​StaticHost.Tests/​Live/​LiveEndpointsTests.cs Adapts the test server for WebSockets.
src/​statichost/​StaticHost/​Program.cs Enables WebSocket middleware.
src/​statichost/​StaticHost/​Live/​README.md Documents transport behavior and deployment requirements.
src/​statichost/​StaticHost/​Live/​LiveStatusServiceCollectionExtensions.cs Updates transport terminology in comments.
src/​statichost/​StaticHost/​Live/​LiveStatusOptions.cs Updates connection-lifetime documentation.
src/​statichost/​StaticHost/​Live/​LiveStatusBroadcaster.cs Serializes shared WebSocket state messages.
src/​statichost/​StaticHost/​Live/​LiveStatus.cs Updates subscriber terminology in comments.
src/​statichost/​StaticHost/​Live/​LiveEndpoints.cs Replaces SSE with the WebSocket endpoint.
src/​statichost/​StaticHost/​Extensions.cs Excludes WebSocket requests from tracing.
src/​frontend/​tests/​unit/​live-status.vitest.test.ts Tests reconnects, polling, and heartbeat detection.
src/​frontend/​tests/​unit/​astro-vite-config.vitest.test.ts Checks WebSocket proxy configuration.
src/​frontend/​tests/​e2e/​live-status.spec.ts Migrates live-status browser mocks to WebSockets.
src/​frontend/​tests/​e2e/​live-pip.spec.ts Updates picture-in-picture transport mocking.
src/​frontend/​tests/​e2e/​live-embeds.spec.ts Updates embedded-player transport mocking.
src/​frontend/​tests/​e2e/​live-accessibility.spec.ts Updates accessibility-test transport mocking.
src/​frontend/​src/​components/​live-status.ts Implements WebSocket updates and recovery behavior.
src/​frontend/​astro.config.mjs Enables WebSocket forwarding in development.
src/​apphost/​Aspire.Dev.AppHost/​LiveExtensions.cs Updates the dashboard endpoint link.
src/​apphost/​Aspire.Dev.AppHost/​AppHost.cs Enables App Service WebSockets.

Review effort: Balanced

@aspire-repo-bot

Copy link
Copy Markdown
Contributor

Frontend HTML artifact ready

The latest frontend build uploaded the frontend-dist artifact for PR #1942. Use the VS Code button below to open this PR with GitHub Artifacts Explorer and browse the built HTML locally.

VS Code: Open PR #1942 artifacts

This comment updates automatically when a new frontend build artifact is uploaded.

Call seed function when document is visible after socket loss

Co-authored-by: Copilot Autofix powered by AI <[email protected]>
@IEvangelist
David Pine (IEvangelist) merged commit d4f9fa5 into main Oct 23, 2026
20 checks passed
@IEvangelist
David Pine (IEvangelist) deleted the ievangelist-live-status-websocket branch October 23, 2026 18:17
Sébastien Ros (sebastienros) added a commit that referenced this pull request Nov 11, 2026
- TypeScript API parameter descriptions (#2009)
- No locale prefix or hreflang alternates for English-only pages: the footer
  language picker (#1988), hreflang (#1987) and the sidebar Glossary link (#1989)
- Footer link to the Aspire MIT license
- Homepage: environment fade (#1993), stale AppHost animations (#1950), the
  dashboard carousel keeps the selected slide on activation
- Search: first-result highlight with out-of-order renders (#2007), hover cursor
  flicker (#1977)
- Live status over WebSocket (#1942) and twoslash hover, merged into the copies
  of the frontend scripts
- Icon data 5.40.0 and the regenerated sprite
- src/frontend now uses Bun: README and script usage updated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants