Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:0xK3vin MegaMemory Live Graph Visualization

From Leeroopedia
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:

St=(Nt,Et,Ut)

where Nt is the set of active node IDs, Et is the set of edge keys ("from|to|relation"), and Ut maps node IDs to updated_at timestamps. On each poll:

Δ=(added=Nt+1∖Nt,removed=Nt∖Nt+1,updated={i∈Nt∩Nt+1:Ut+1(i)≠Ut(i)})

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

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment