feat: SSE live ping stream for monitors

This commit is contained in:
M1
2026-03-16 16:14:23 +04:00
parent 1e95149456
commit 6d48a83560
4 changed files with 146 additions and 8 deletions
+18
View File
@@ -93,3 +93,21 @@ function escapeHtml(str) {
div.textContent = str;
return div.innerHTML;
}
// Subscribe to live ping updates for a monitor via SSE
// onPing(ping) called with each new ping object
function watchMonitor(monitorId, onPing) {
const key = localStorage.getItem('pingql_key');
if (!key) return null;
const url = `/monitors/${monitorId}/stream`;
const es = new EventSource(url + `?auth=${encodeURIComponent(key)}`);
es.onmessage = (e) => {
try { onPing(JSON.parse(e.data)); } catch {}
};
es.onerror = () => {
// Reconnect is automatic with EventSource
};
return es;
}