Next.jsWebSocketCloud GamingStripe PromptPaySystem Architecture

Architecting NexiCloud: Real-Time Cloud Gaming Rental with Next.js, WebSocket Signaling, and Wake-on-LAN Orchestration

เบื้องหลังสถาปัตยกรรม NexiCloud: ระบบเช่าเครื่อง Cloud Gaming ความหน่วงต่ำด้วย Next.js, WebSocket และ Wake-on-LAN

Author: Patipan Plonying···7 min read

1. Engineering Challenges in On-Demand Cloud Gaming Rental

When building NexiCloud (https://nexicloud.company/), the primary goal was to enable players to rent high-end gaming PCs on demand and stream AAA titles at up to 4K 60fps with ultra-low latency (~1ms local network overhead) via the Moonlight and Sunshine protocols across Windows, macOS, Android, and iOS/iPadOS.

The core engineering challenge solved by Patipan Plonying went beyond a standard booking UI: it required tightly orchestrating three real-time subsystems—(1) an automated wallet and instant QR top-up ledger, (2) a time-sensitive WebSocket signaling channel for 4-digit PIN pairing with Host PCs, and (3) remote hardware power management and post-session environment sanitization.

2. Real-Time PIN Pairing & Session Signaling over WebSockets

Once a player reserves a machine and opens the Moonlight client, the web platform must relay a 4-digit pairing PIN to the target Host PC immediately and stream status updates back to the browser without manual refreshes. We engineered a resilient WebSocket Signaling Client with exponential backoff and random jitter to prevent thundering-herd reconnection spikes during network blips.

src/lib/signaling-ws.tstypescript
// Exponential Backoff + Jitter reconnection delay calculation
function getReconnectDelayMs(attempt: number): number {
  const baseDelay = Math.min(1000 * Math.pow(2, attempt), 30000);
  const jitter = 0.75 + 0.5 * Math.random();
  return Math.floor(baseDelay * jitter);
}

// Relay 4-digit Moonlight pairing PIN to the Host PC over WebSocket
export function sendLaunchTrigger(ws: WebSocket | null, bookingId: string, pin: string) {
  if (ws && ws.readyState === WebSocket.OPEN) {
    ws.send(
      JSON.stringify({
        type: "launch_session",
        bookingId,
        pin,
      })
    );
  }
}

3. Automated Stripe PromptPay QR Top-Ups & Decimal-Precision Audit Ledger

To provide frictionless onboarding starting at 9 THB/hour, the integrated Wallet connects with Stripe PromptPay to generate scannable Thai banking QR codes on the fly, verifying webhook events and crediting player balances automatically within 3–5 seconds.

Every top-up and rental deduction is recorded in a centralized financial Audit Ledger linked to its Gateway Reference ID, using strict decimal precision arithmetic to eliminate floating-point rounding discrepancies.

4. Remote Hardware Orchestration (Wake-on-LAN & SSH) and Security Hardening

On the operations side, we built an Admin Console capable of waking sleeping Host PCs over the network via Wake-on-LAN (MAC address magic packets) and executing remote reboots or shutdowns over SSH directly from the browser, paired with a Host Agent that reports live CPU, GPU, and RAM telemetry.

Administrative access is hardened with TOTP 2FA (Authenticator app), httpOnly cookie token storage to mitigate XSS, and automated post-session quarantine routines that wipe previous game account sessions before the next rental begins.