Rendering 1,000+ Character Constellation Nodes on HTML5 Canvas & Decoupling AI SSE Streams from Laravel
Building an encyclopedia and interactive world-building companion for long-running Eastern fantasy, Xianxia, and Donghua series (often spanning 2,000–4,000+ chapters) introduces a surprisingly fun set of frontend and bac
Building an encyclopedia and interactive world-building companion for long-running Eastern fantasy, Xianxia, and Donghua series (often spanning 2,000–4,000+ chapters) introduces a surprisingly fun set of frontend and backend engineering problems.
While building VozNovel—which now organizes over 37,000 character profiles, cultivation realms, factions, and an interactive AI Storyteller—we had to keep everything smooth on mobile browsers and a modest VPS without letting heavy graph views or streaming AI turns lock up web workers.
Here are the three architectural choices that made the biggest difference.
1. Moving the Relationship Graph from DOM/SVG to HTML5 <canvas>
For small graphs (<250 entities), SVG or HTML `
` nodes work well. Once we mapped massive cultivation worlds like those in our VozNovel Character Encyclopedia or dense faction trees such as Kiếm Lai (Sword of Coming) and Cổ Chân Nhân (Reverend Insanity), DOM/SVG hit mobile bottlenecks fast:- Recalculating layout and styles across 1,000+ positioned labels during pan/zoom.
- Re-rasterizing hundreds of curved relationship Bézier edges on every touch frame.
Switching to a dedicated HTML5 <canvas> renderer solved this with three practical steps:
-
Viewport Culling: On each
requestAnimationFrame, project viewport bounds into world coordinates and skip drawing any node or edge outsideworldBounds + padding. -
Spatial Hit-Testing: Instead of attaching thousands of DOM listeners, transform
pointermove/clickcoordinates from screen space back to world space and look up nearby nodes via a lightweight spatial grid in ~O(1) time. -
Clamped DPR & Adaptive Labels: Clamp canvas scaling to
Math.min(window.devicePixelRatio || 1, 2)so high-density mobile screens stay crisp without ballooning GPU memory, and suppress secondary labels whileisPanningis active.
2. Single-Flight Stale Cache & Guest Session Isolation on Laravel + Redis
Directory and character pages receive spiky community traffic. If hundreds of concurrent visitors hit an expired Redis key at the same moment (cache stampede), database queries pile up immediately.
We added two lightweight safeguards:
-
Single-Flight Lock (
Cache::lock) +stale-while-revalidate: Only one request acquires the lock to rebuild fresh HTML afterfresh_untilexpires; concurrent visitors receive the cached stale response from Redis in under 2ms while regeneration finishes. -
Anonymous Read Session Skip: Read-only public encyclopedia routes switch Laravel's session driver to
arrayat runtime and strip redundantSet-Cookieheaders for guests, preventing tens of thousands of orphan session keys in Redis every day.
3. Decoupling Long-Lived AI SSE Streams from PHP Workers
Our AI Storyteller mode keeps a Server-Sent Events (SSE) stream open for 15–60 seconds per interactive story turn while streaming tokens back to the reader. Holding PHP-FPM / LSAPI workers open on slow network sockets quickly exhausts max_children under concurrency.
We separated the lifecycle into a two-step Stream Ticket Handoff:
- Laravel handles auth, story memory/canon validation, and quota checks in <30ms, then writes a short-lived signed HMAC Stream Ticket into Redis.
- The browser connects directly to a lightweight async SSE proxy with that ticket so PHP workers immediately return to the pool to serve regular SSR pages under 50ms.
If you enjoy cultivation/Xianxia worldbuilding, Donghua character lore, or canvas graph performance challenges, feel free to explore VozNovel—and I’d love to hear how you handle large-node canvas rendering or SSE handoffs in your own stack!
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.