Principle:0xK3vin MegaMemory Live Graph Visualization
| Knowledge Sources | |
|---|---|
| Domains | Visualization, Real_Time, HTTP |
| Last Updated | 2026-10-27 12:00 GMT |
Overview
A visualization technique that renders a knowledge graph as an interactive force-directed layout and pushes live updates to connected clients via Server-Sent Events.
Description
Live graph visualization serves two concerns: rendering the graph as an interactive visual (nodes positioned by a force simulation, edges as links) and keeping the view synchronized as the graph changes without requiring a page refresh. The rendering uses a force-directed layout (d3-force) where nodes repel each other and edges act as springs, producing a readable layout. Live updates use Server-Sent Events (SSE): the server polls the database on an interval, diffs against the last-known state, and broadcasts incremental change events (node_added, node_updated, node_removed, edge_added, edge_removed, stats) to all connected clients.
Problem solved: A static graph view goes stale as the agent records new concepts. Manual refresh is tedious and disrupts the user's flow. SSE-based live updates keep the view current automatically, so a user watching the explorer sees the graph grow in real time as the agent works.
Context: MegaMemory's web explorer (src/web.ts) serves a bundled single-file HTML frontend and an SSE endpoint (/api/events). On connect, the client receives an init event with the full graph payload and stats. The server then polls the DB every 1.5s via detectChanges, which diffs node IDs, updated_at timestamps, and edge keys, broadcasting only what changed. A heartbeat comment is sent every 30s to keep the connection alive. Dead clients are filtered out of the sseClients array.
Usage
Use this principle when:
- You want users to watch a graph grow or change in real time
- Polling the DB is acceptable (vs. push-based change notifications)
- A force-directed layout is appropriate for your graph's size and structure
- SSE is preferable to WebSockets (unidirectional, simpler, HTTP-compatible)
Theoretical Basis
Change detection: The server maintains a snapshot of known state:
where is the set of active node IDs, is the set of edge keys ("from|to|relation"), and maps node IDs to updated_at timestamps. On each poll:
Each element of becomes an SSE event broadcast to all clients.
SSE protocol:
data: {"type":"init","data":{"nodes":[...],"edges":[...],"stats":{...}}}\n\n
data: {"type":"node_added","data":{"id":"new-concept",...}}\n\n
: heartbeat\n\n
Pseudo-code:
# Abstract change detection (NOT real implementation)
def detect_changes():
current = read_db_state()
for id in current.nodes - last_known.nodes:
broadcast("node_added", current.nodes[id])
for id in last_known.nodes - current.nodes:
broadcast("node_removed", id)
for id in current.nodes & last_known.nodes:
if current.updated_at[id] != last_known.updated_at[id]:
broadcast("node_updated", current.nodes[id])
last_known = current