-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
258 lines (244 loc) · 9.8 KB
/
Copy pathindex.html
File metadata and controls
258 lines (244 loc) · 9.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>teesql-example — read/write split demo</title>
<style>
* { box-sizing: border-box; }
body {
font-family: ui-monospace, "SF Mono", Menlo, monospace;
max-width: 880px;
margin: 2em auto;
padding: 0 1em;
color: #111;
background: #fafafa;
-webkit-text-size-adjust: 100%;
}
h1 { font-size: 1.2em; margin: 0 0 0.5em; }
.sub { color: #666; font-size: 0.85em; margin-bottom: 1.5em; line-height: 1.4; }
.row { display: flex; gap: 0.5em; margin-bottom: 1em; flex-wrap: wrap; }
input, button {
font-family: inherit;
font-size: 16px; /* 16px stops iOS Safari zoom-on-focus */
padding: 0.5em 0.6em;
border: 1px solid #bbb;
border-radius: 3px;
background: #fff;
min-width: 0;
}
input { flex: 1 1 12em; }
button {
cursor: pointer;
background: #111;
color: #fff;
border-color: #111;
flex: 0 0 auto;
}
button:hover { background: #333; }
button:disabled { background: #999; cursor: not-allowed; }
#status { font-size: 0.8em; color: #666; margin-bottom: 1em; }
#status.connected { color: #060; }
#status.disconnected { color: #c00; }
#log {
border: 1px solid #ddd;
background: #fff;
border-radius: 3px;
padding: 0.5em;
max-height: 60vh;
overflow-y: auto;
-webkit-overflow-scrolling: touch;
font-size: 0.82em;
line-height: 1.4;
}
/* Two-row event card on every screen — meta header + payload below.
* Lets long payloads wrap without forcing a horizontal scrollbar. */
.ev {
border-bottom: 1px solid #eee;
padding: 0.45em 0.3em;
display: grid;
grid-template-columns: minmax(2.5em, auto) 1fr auto;
grid-template-areas: "id topic ts" "pl pl pl";
column-gap: 0.7em;
row-gap: 0.15em;
}
.ev:last-child { border-bottom: none; }
.ev .id { grid-area: id; color: #666; }
.ev .topic {
grid-area: topic;
font-weight: bold;
min-width: 0;
overflow-wrap: anywhere;
}
.ev .ts {
grid-area: ts;
color: #888;
font-size: 0.92em;
white-space: nowrap;
}
.ev .payload {
grid-area: pl;
color: #333;
overflow-wrap: anywhere;
word-break: break-word;
}
.err { color: #c00; }
@media (max-width: 540px) {
body { margin: 1em auto; padding: 0 0.6em; }
h1 { font-size: 1.1em; }
.sub { font-size: 0.8em; }
#log { padding: 0.3em; font-size: 0.78em; }
.ev {
column-gap: 0.5em;
grid-template-columns: minmax(2em, auto) 1fr;
grid-template-areas: "id topic" "ts ts" "pl pl";
}
.ev .ts { font-size: 0.78em; }
button { width: 100%; } /* full-width submit on phones */
}
</style>
</head>
<body>
<h1>teesql-example</h1>
<div class="sub">
Writes go to the cluster's primary; reads stream from a polling
subscription on the secondary. The page loads the most recent
history, then renders new events as they replicate. Open in two
tabs and watch one tab's writes show up in the other.
</div>
<div id="status" class="disconnected">connecting…</div>
<form id="form" class="row">
<input id="topic" placeholder="topic" value="demo" autocomplete="off">
<input id="payload" placeholder='{"hello": "world"}' value='{"hello":"world"}' autocomplete="off">
<button type="submit">POST /events</button>
</form>
<div id="log"></div>
<script>
const log = document.getElementById('log');
const status = document.getElementById('status');
const form = document.getElementById('form');
const topicEl = document.getElementById('topic');
const payloadEl = document.getElementById('payload');
// Track ids we've already rendered so a live event that overlaps
// with the history fetch doesn't get drawn twice.
const seenIds = new Set();
// Buffer live events received before history finishes loading,
// then drain them after history is in the DOM.
let buffered = [];
let historyLoaded = false;
const HISTORY_LIMIT = 200;
const MAX_DOM_ROWS = 500;
function setStatus(msg, ok) {
status.textContent = msg;
status.className = ok ? 'connected' : 'disconnected';
}
function buildRow(ev) {
const row = document.createElement('div');
row.className = 'ev';
const idEl = document.createElement('div'); idEl.className = 'id'; idEl.textContent = '#' + ev.id;
const tsEl = document.createElement('div'); tsEl.className = 'ts'; tsEl.textContent = ev.recorded_at;
const topicEl_ = document.createElement('div'); topicEl_.className = 'topic'; topicEl_.textContent = ev.topic;
const payloadEl_ = document.createElement('div'); payloadEl_.className = 'payload';
payloadEl_.textContent = JSON.stringify(ev.payload);
row.appendChild(idEl); row.appendChild(tsEl); row.appendChild(topicEl_); row.appendChild(payloadEl_);
return row;
}
// Live events go to the top (newest at top). Cap from the bottom.
function prependEvent(ev) {
if (seenIds.has(ev.id)) return;
seenIds.add(ev.id);
log.insertBefore(buildRow(ev), log.firstChild);
while (log.children.length > MAX_DOM_ROWS) log.removeChild(log.lastChild);
}
// History rows arrive newest-first; appendChild keeps that order
// (newest history at the top of the history block, oldest at the
// bottom). Live events still prepend, so they sit above all history.
function appendHistoryEvent(ev) {
if (seenIds.has(ev.id)) return;
seenIds.add(ev.id);
log.appendChild(buildRow(ev));
while (log.children.length > MAX_DOM_ROWS) log.removeChild(log.firstChild);
}
function appendError(msg) {
const row = document.createElement('div');
row.className = 'ev err';
row.textContent = msg;
log.insertBefore(row, log.firstChild);
}
async function loadHistory() {
try {
const r = await fetch('/events?limit=' + HISTORY_LIMIT);
if (!r.ok) throw new Error('HTTP ' + r.status);
const rows = await r.json();
for (const ev of rows) appendHistoryEvent(ev);
} catch (e) {
appendError('history fetch failed: ' + e);
} finally {
// Drain any live events that arrived while we were fetching.
// They're newer than (or overlap with the latest of) history,
// so they belong at the top.
const drained = buffered;
buffered = [];
historyLoaded = true;
for (const ev of drained) prependEvent(ev);
}
}
function openWebSocket() {
const proto = location.protocol === 'https:' ? 'wss:' : 'ws:';
const ws = new WebSocket(proto + '//' + location.host + '/ws/events');
ws.onopen = () => {
setStatus('connected — listening for events', true);
// First-ever connection: fetch history. On reconnect we
// skip — the operator can refresh the page if they want
// to fill a gap caused by a prolonged disconnect.
if (!historyLoaded) loadHistory();
};
ws.onclose = () => {
setStatus('disconnected — retrying in 1s', false);
setTimeout(openWebSocket, 1000);
};
ws.onerror = (e) => console.warn('ws error', e);
ws.onmessage = (msg) => {
let ev;
try {
ev = JSON.parse(msg.data);
} catch (e) {
appendError('parse error: ' + e);
return;
}
if (!historyLoaded) {
buffered.push(ev);
} else {
prependEvent(ev);
}
};
}
openWebSocket();
form.addEventListener('submit', async (e) => {
e.preventDefault();
let payload;
try {
payload = JSON.parse(payloadEl.value);
} catch (err) {
appendError('payload is not valid JSON: ' + err);
return;
}
try {
const r = await fetch('/events', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ topic: topicEl.value, payload }),
});
if (!r.ok) {
appendError('POST /events ' + r.status + ': ' + await r.text());
}
// We DON'T append here — wait for the WebSocket to deliver
// it from the secondary, so we're actually demonstrating
// the read/write split.
} catch (err) {
appendError('POST failed: ' + err);
}
});
</script>
</body>
</html>