Course lesson 43 · Advanced Durable Objects
Realtime link clicks with Websockets
The tracker now uses an alarm to batch clicks before broadcasting them to connected browsers. Each new click is stored, and if no alarm exists one is scheduled roughly two seconds ahead; the alarm queries a bounded recent set, sends it, advances offsets, and removes older rows. The instructor verifies the full redirect-to-map flow using local clicks, a Malaysia VPN location, and a phone.
Primary source: course video 43 · Supplied English subtitles · 00:17:32.
Understand the idea
- Batching trades a small display delay for fewer WebSocket messages when many clicks arrive together.
- Durable storage preserves offset values across object restarts; in-memory fields make them convenient during execution.
- A query helper returns click records plus newest and oldest times, so delivery and cleanup can share boundaries.
- A shared Zod schema defines the shape of click data sent to the client.
- The map is a bounded recent view, while the existing queue path remains responsible for the broader analytics flow.
What the course does
- Set an alarm only when one is not already scheduled, using a two-second target.
- Load offset state during object initialization, defaulting missing values to zero.
- Create a parameterized query helper for clicks after an offset, with a default result limit of 50.
- On alarm, query recent clicks and broadcast the click array to all connected sockets.
- Persist updated boundary times and delete records older than the retained boundary.
- Temporarily connect the frontend to the deployed secure socket, test geographic clicks, then restore host configuration.
Watch for
- The instructor presents the batching policy as an illustrative solution, not an optimal implementation for every workload.
- Only a bounded subset of a burst is intended for visualization; do not read the map as an exhaustive event ledger.
- Added advice: timestamp-only offsets deserve collision and ordering tests before relying on them for lossless delivery.
Recall before revealing
Why send the batch before performing cleanup?
Reveal the explanation
The lesson prioritizes getting the selected clicks to viewers promptly, then updates offsets and trims retained history.
Try it
Walk through a burst of 200 clicks with a 50-record query limit and explain the map’s purpose.
Check your result
You describe bounded visualization and identify the separate queue path instead of claiming all 200 clicks must appear individually.
Practice prompts and answer checks are added teaching material. Answer from memory first, then compare and explain any difference.
Rewatch only what you need
Open this lesson in your original course library and seek to the times below. Videos are not hosted on this site.
- 00:01:31 — Two-second batching alarm
- 00:05:03 — Persistent offset initialization
- 00:06:35 — Recent-click query helper
- 00:10:39 — Removing older retained clicks
- 00:14:47 — Malaysia redirect and map verification
Companion primary documentation: Durable Objects WebSockets. For recorded API names, commands, limits, and prices, check the version you use.