| 1 |
<!DOCTYPE html> |
| 2 |
<html> |
| 3 |
<head> |
| 4 |
<meta name="htmx-config" content='{"logAll":true, "extensions":"sse"}'> |
| 5 |
<script src="/htmx.js"></script> |
| 6 |
<script src="/ext/hx-sse.js"></script> |
| 7 |
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script> |
| 8 |
</head> |
| 9 |
<body class="group max-w-5xl mx-auto p-4 my-12"> |
| 10 |
<h1 class="text-xl font-bold mb-4"> |
| 11 |
Server-Sent Events (SSE) Test |
| 12 |
</h1> |
| 13 |
|
| 14 |
|
| 15 |
<section class="mb-12"> |
| 16 |
<h2 class="text-lg font-semibold mb-2">1. One-shot Stream</h2> |
| 17 |
<p class="text-sm text-neutral-600 mb-4"> |
| 18 |
Server sends multiple messages, then closes. Each message replaces the content. |
| 19 |
</p> |
| 20 |
|
| 21 |
<button |
| 22 |
hx-get="/matrix-stream" |
| 23 |
hx-swap="innerHTML" |
| 24 |
hx-target="#matrix-output" |
| 25 |
class="px-4 py-2 bg-blue-600 text-white rounded hover:bg-blue-700 mb-4 cursor-pointer"> |
| 26 |
Start Stream |
| 27 |
</button> |
| 28 |
|
| 29 |
<div id="matrix-output" class="fixed inset-0 bg-black font-mono font-bold text-xl text-green-500 invisible has-[.matrix-text]:visible content-center text-center transition-all"></div> |
| 30 |
</section> |
| 31 |
|
| 32 |
|
| 33 |
<section class="mb-12"> |
| 34 |
<h2 class="text-lg font-semibold mb-2">2. Persistent Stream</h2> |
| 35 |
<p class="text-sm text-neutral-600 mb-4"> |
| 36 |
Long-lived connection. Server sends <code><partial></code> tags to update multiple targets. |
| 37 |
</p> |
| 38 |
|
| 39 |
<button |
| 40 |
hx-get="/events" |
| 41 |
hx-target="#events-output" |
| 42 |
hx-swap="innerHTML" |
| 43 |
hx-config="sse.reconnect:true sse.reconnectMaxAttempts:3 sse.reconnectDelay:0 sse.reconnectMaxDelay:10s sse.pauseOnBackground:true" |
| 44 |
class="px-4 py-2 bg-purple-600 text-white rounded hover:bg-purple-700 mb-4"> |
| 45 |
Connect |
| 46 |
</button> |
| 47 |
|
| 48 |
<div id="events-output" class="fixed inset-0 bg-neutral-950 text-white invisible has-[.events-active]:visible font-mono"> |
| 49 |
<div id="activity" class="absolute inset-0 flex items-center justify-center text-sm opacity-30 max-w-md mx-auto space-y-3 flex-col mask-y-from-80% mask-y-to-100%"></div> |
| 50 |
<div class="fixed bottom-8 left-1/2 -translate-x-1/2 flex items-center gap-3 bg-neutral-900 px-4 py-2 rounded-full border border-neutral-800"> |
| 51 |
<div id="status-indicator" class="w-2 h-2 rounded-full bg-neutral-500"></div> |
| 52 |
<div id="system-status" class="text-sm"></div> |
| 53 |
</div> |
| 54 |
</div> |
| 55 |
</section> |
| 56 |
|
| 57 |
|
| 58 |
<section class="mb-12"> |
| 59 |
<h2 class="text-lg font-semibold mb-2">3. Custom Events from SSE</h2> |
| 60 |
<p class="text-sm text-neutral-600 mb-4"> |
| 61 |
SSE <code>event:</code> fields trigger custom DOM events. Listen with <code>hx-on:</code> attributes. |
| 62 |
</p> |
| 63 |
|
| 64 |
<button |
| 65 |
hx-get="/progress-stream" |
| 66 |
hx-on:progress="htmx.find('#bar').style.width = event.detail.data + '%'; htmx.find('#pct').textContent = event.detail.data + '%'" |
| 67 |
hx-on:done="this.textContent = 'Complete!'" |
| 68 |
class="px-4 py-2 bg-green-600 text-white rounded hover:bg-green-700 mb-3 cursor-pointer"> |
| 69 |
Start Progress |
| 70 |
</button> |
| 71 |
|
| 72 |
<div class="flex items-center gap-2 mt-4"> |
| 73 |
<div class="flex-1 bg-neutral-200 rounded-full h-4 overflow-hidden"> |
| 74 |
<div id="bar" class="bg-green-500 h-full transition-all duration-100" style="width: 0%"></div> |
| 75 |
</div> |
| 76 |
<span id="pct" class="text-sm font-mono w-12 text-right">0%</span> |
| 77 |
</div> |
| 78 |
</section> |
| 79 |
|
| 80 |
<footer class="mt-12 pt-6 border-t border-neutral-200 text-sm text-neutral-500"> |
| 81 |
<p>Check browser console for htmx debug logs</p> |
| 82 |
</footer> |
| 83 |
</body> |
| 84 |
</html> |