Zoo

← back to the zoo

10. Live for everyone

A change one person makes appears on everyone's open page within a second, without a reload.

Declare a channel in ratcloud.conf; browsers listen with EventSource at /rc/sub/<name>/<id>, the app sends with rc.publish(name, id, data).

This is the code serving the zoo right now: the bytes this very program was built from (each file is embedded when the zoo is compiled), at commit 55c71513c5 (live). The zoo's own self-check fetches this page and compares it byte for byte with the file it runs.

e10_counter.odin sha256 5ad2b689d36a · raw

1package main
2
3// Exhibit 10. A channel is declared in ratcloud.conf (`channels = counter, ...`). Browsers listen at
4// /rc/sub/counter/<id> with EventSource; the app publishes with rc.publish("counter", id, data). That
5// is the whole of realtime.
6
7import "core:encoding/json"
8import "core:fmt"
9import "core:time"
10import "ratcloud:rc"
11
12e10 :: proc() -> Exhibit {
13	return {
14		n = 10, try_ = "press the shared counter", wing = "Realtime",
15		title = "Live for everyone",
16		promise = "A change one person makes appears on everyone's open page within a second, without a reload.",
17		block = "Declare a channel in ratcloud.conf; browsers listen with EventSource at /rc/sub/<name>/<id>, the app sends with rc.publish(name, id, data).",
18		src = {"e10_counter.odin", #load("e10_counter.odin", string)},
19		script = {"web/e10.js", #load("web/e10.js", string)},
20		show = proc(me: ^rc.User) -> string {
21			return fmt.tprintf(`<div class="row"><span class="big" id="counter">%d</span><button class="primary" id="press">Press</button></div><p class="muted out" id="counter-note">Open this page in two tabs and press in one.</p>`, meta_int("counter"))
22		},
23		api = proc(a: ^Api) -> bool {
24			if a.do_ != "press" || !a.post do return false
25			must("BEGIN IMMEDIATE")
26			n := meta_int("counter") + 1
27			meta_set("counter", itoa(n))
28			must("COMMIT")
29			ok := publish("counter", "main", fmt.tprintf(`{{"n":%d,"at":%.3f}}`, n, f64(now_ms()) / 1000))
30			reply_raw(a, fmt.tprintf(`{{"n":%d,"published":%v}}`, n, ok))
31			return true
32		},
33		// Two listeners, one press: both must see the new number within a second.
34		check = proc() -> Check {
35			sub := self_url("/rc/sub/counter/main")
36			b := new_browser()
37			l := listen_open(b, {sub, sub}, 2000)
38			defer listen_close(&l)
39			t0 := time.tick_now()
40			r := post(b, self_url("/api/10/press"))
41			n, has := jint(r.j, "n")
42			if !has do return fail("pressing the counter failed")
43			if l.status[0] != 200 || l.status[1] != 200 do return fail(fmt.tprintf("the listeners were answered %d, %d", l.status[0], l.status[1]))
44			want :: proc(v: json.Value, arg: rawptr) -> bool {return jint(v, "n") == (^i64)(arg)^}
45			seen, ms := listen_wait(&l, t0, want, &n)
46			if seen < 2 do return fail(fmt.tprintf("pressed to %d; only %d of 2 listeners saw it within 2 seconds", n, seen))
47			if ms > 1000 do return fail(fmt.tprintf("both listeners saw %d, but after %dms", n, ms))
48			return pass(fmt.tprintf("pressed to %d; both listeners saw it %dms later (press included)", n, ms))
49		},
50	}
51}

web/e10.js sha256 e408704762f7 · raw

1(() => {
2  const el = document.getElementById("counter"), note = document.getElementById("counter-note");
3  if (!el) return;
4  const es = new EventSource("/rc/sub/counter/main");
5  es.onmessage = (m) => { const j = JSON.parse(m.data); el.textContent = j.n; };
6  es.onerror = () => { note.textContent = "Lost the live connection; retrying…"; };
7  es.onopen = () => { note.textContent = "Live. Open this page in another tab and press in one."; };
8  document.getElementById("press").addEventListener("click", () => zoo.call("/api/10/press", {}));
9})();