Back to KB
Difficulty
Intermediate
Read Time
8 min

Building a live 3D globe of real time web traffic with Three.js and Server Sent Events

By Codcompass TeamΒ·Β·8 min read

Real-Time Geospatial Visualization: Optimizing Three.js Particle Systems for Live Data Streams

Current Situation Analysis

Modern web applications increasingly demand immersive, real-time data visualization. A common requirement is displaying live activity on a 3D globe, such as tracking user visits, server loads, or transaction flows. However, implementing this efficiently is frequently mishandled.

The primary pain point is rendering jank caused by unbounded resource allocation. Many developers approach real-time updates by spawning new mesh objects or dynamically resizing geometry arrays for every incoming event. This pattern triggers frequent garbage collection cycles, causes memory leaks, and leads to severe frame rate drops on mobile devices. The misconception is that Three.js handles dynamic geometry updates efficiently by default; in reality, modifying BufferAttribute sizes or creating new geometries per frame forces the GPU to reallocate buffers, breaking the render loop's stability.

Data from production deployments shows that naive implementations often struggle to maintain 30 FPS on mid-range Android devices due to GC pauses. Conversely, systems utilizing fixed-size buffer pools and edge-side aggregation can sustain 60 FPS on high-end mobile hardware while keeping memory footprint constant. The solution requires decoupling data ingestion from rendering by treating the visualization as a state machine operating on a pre-allocated resource pool.

WOW Moment: Key Findings

The performance delta between dynamic allocation and fixed-buffer management is substantial. The following comparison highlights the impact of architectural choices on rendering stability and resource usage.

ApproachGC PressureFrame Rate (Mobile)Memory StabilityLatency Impact
Dynamic Mesh SpawningHigh< 30 FPSUnbounded GrowthHigh (Main Thread Block)
Naive Array PushMedium~45 FPSLinear GrowthMedium
Fixed Buffer PoolNone60 FPSConstantLow

Why this matters: The fixed buffer approach transforms the visualization from an event-driven creation process into a deterministic update loop. By pre-allocating memory and reusing slots via a circular buffer, the application eliminates allocation overhead entirely. This enables smooth animations even under high-throughput data streams and ensures predictable performance across diverse client devices.

Core Solution

Building a performant live globe requires a coordinated stack: edge-side aggregation to reduce payload frequency, a fixed-buffer particle system in Three.js, and custom GLSL shaders for visual effects.

1. Architecture: Edge Aggregation and SSE

Streaming every single event directly to the client is inefficient. Instead, aggregate events at the edge.

  • Data Flow: Client tracking script β†’ Cloudflare Worker β†’ Cloudflare Queue β†’ Consumer Worker β†’ SSE Stream β†’ Browser.
  • Batching: The consumer worker aggregates events per region every 2 seconds. This reduces the number of SSE messages and allows the client to process batches rather than individual spikes.
  • Protocol: Server-Sent Events (SSE) are preferred over WebSockets for this use case. SSE provides automatic reconnection, works over standard HTTP, and is sufficient for unidirectional data flow.

2. Coordinate Conversion

Geographic coordinates must be mapped to 3D Cartesian space. The conversion must account for the sphere's radius and the coordinate system orientation.

// Converts latitude/longitude to 3D vector on a sphere
function mapGeographicToCartesian(
  latitude: number,
  longitude: number,
  sphereRadius: number
): [number, number, number] {
  cons

πŸŽ‰ Mid-Year Sale β€” Unlock Full Article

Base plan from just $4.99/mo or $49/yr

Sign in to read the full article and unlock all 635+ tutorials.

Sign In / Register β€” Start Free Trial

7-day free trial Β· Cancel anytime Β· 30-day money-back