Skip to Content
πŸ–₯️ Self-HostingReal-Time Collaboration & Preview

Real-Time Collaboration & Preview

Sitepins provides two advanced real-time systems:

  1. Real-Time Collaboration: Powered by Yjs CRDTs over Hocuspocus WebSockets and Socket.IO presence gateways.
  2. Interactive Live Preview: Runs user site generators (Astro, Next.js, Hugo, etc.) inside isolated sandbox VMs.

1. Real-Time Collaboration Architecture

Two independent real-time layers run concurrently on the Express server:

SystemProtocolPathData Transferred
Hocuspocus (Yjs)Raw WebSocket (ws)/api/v1/editor/collabDocument content: Yjs CRDT delta operations, remote awareness carets, and inline cursor positions.
Socket.IOEngine.IO / Polling / WS/socket.io/Session metadata: Who is looking at a file, user avatars, and commit notifications.
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Plate Visual Editor β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ Yjs CRDT Operations β”‚ Awareness & Commits β–Ό β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Hocuspocus Server β”‚ β”‚ Socket.IO Server β”‚ β”‚ (/api/v1/editor/collab) β”‚ β”‚ (/socket.io/) β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ Document Authorization β”‚ β”‚ Presence Gateway β”‚ β”‚ In-Memory CRDT Debounce β”‚ β”‚ Commit Gateway β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ β–Ό β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ MongoDB Database β”‚ β”‚ (Tenant Membership Validation: isOrgMember) β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Document Naming & Tenant Isolation

Document names follow a strict virtual path:

/org-<orgId>/<projectId>/<filePath>

When a client initiates a WebSocket handshake to Hocuspocus:

  1. The server reads the HTTP session cookie from the upgrade request headers.
  2. It parses the <orgId> segment from the document URI.
  3. It queries MongoDB using isOrgMember(userId, orgId).
  4. If the user is not a verified member of that organization, the socket is immediately terminated with 403 Forbidden.

This ensures multi-tenant security: a user cannot inspect or edit another organization’s files by simply guessing document paths.

Presence Gateway

The presence gateway (api/src/modules/common/presence.gateway.ts) tracks active viewers:

  • Room Key: presence:<orgId>:<projectId>:<filePath>
  • Multi-Tab Deduplication: If a user opens the same document across multiple browser tabs, their user avatar is displayed once. The user is only removed from the roster when their last open connection closes.
  • Identity Safety: Identity metadata (name, avatar, email) is resolved directly from the authenticated session, not from client-sent payloads.

Commit Notification Gateway

When a collaborator saves or publishes changes, the client emits commit over Socket.IO:

  1. The server validates that the socket has joined the target editor room (join-editor).
  2. The server broadcasts commit:completed with the author’s name, commit SHA, and timestamp to all other active editors in the room.
  3. Other users’ interfaces immediately notify them that the remote file has updated.

2. Live Preview & Sandbox Architecture

Rather than rendering an approximate Markdown preview, Sitepins runs your real static site generator in an isolated Vercel Sandbox (a disposable microVM).

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Browser Editor β”‚ β”‚ (Unsaved Changes) β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ Quick Op (Shell write) β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Next.js API Handler β”‚ β”‚ (/api/sandbox/create) β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ Vercel Sandbox SDK β–Ό β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Vercel Sandbox Linux MicroVM β”‚ β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ β”‚ β”‚ 1. Git clone at branch β”‚ β”‚ β”‚ β”‚ 2. Install toolchain (Node, Go, Hugo) β”‚ β”‚ β”‚ β”‚ 3. Patch dev scripts (0.0.0.0 host binding) β”‚ β”‚ β”‚ β”‚ 4. Run generator dev server (e.g. astro dev) β”‚ β”‚ β”‚ β”‚ 5. Expose public preview URL β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Supported Site Generators

The sandbox system automatically inspects your project configuration and adapts its dev command and toolchain:

GeneratorDefault PortEnvironment & Toolchain Handling
Next.js3000Injects Next.js reload bridge; enables draft environment mode.
Astro4321Patches scripts with --host 0.0.0.0; self-refreshing HMR.
Hugo1313Dynamically installs Go and Hugo extended binary on cold start.
Hugo (exampleSite)1313Accommodates nested theme exampleSite/ root paths.
TanStack Start3000Injects TanStack reload bridge.
SvelteKit / Vite5173Binds to 0.0.0.0 for container reachability.
Jekyll4000Runs static generator dev loop.
Hexo3000Static asset generator mode.

Sandbox Lifecycle & Resiliency Engine

1. Cold Start & Resource Allocation

Triggered when opening preview for the first time:

  1. Allocates 4 vCPUs (8GB RAM) with automatic tier fallback, ensuring Vite, Astro SSR, and image transformations have ample compute without CPU starvation.
  2. Checks MongoDB (/api/v1/project-preview/:projectId) for an active sandbox session.
  3. Clones the repository at the project’s current working branch.
  4. Detects package manager (pnpm, npm, yarn, or bun) and runs install.
  5. Pre-exposes common ports ([port, 1313, 4321, 3000, 5173]) so edge routing is immediately available regardless of framework port shifts (e.g. Hugo on 1313, Astro on 4321, Vite on 5173).
  6. If Hugo is detected, downloads, caches, and verifies the Go and Hugo toolchains before boot to avoid missing-binary crashes.
  7. Patches dev scripts to bind to 0.0.0.0 for external routing.
  8. Starts the dev server with in-container TCP socket health probes (127.0.0.1:${port}) and lightweight HTTP ping probes (/__vite_ping, /favicon.ico).
  9. Monitors /tmp/devserver.log for early crash errors to fail fast (~3s) with actionable diagnostic logs rather than hanging until timeout.
  10. Generates the secure public preview URL.

2. Warm Updates (Quick Ops)

As you type in the editor, Sitepins executes debounced quick updates:

  • Uncommitted edits are streamed directly into the VM’s file system.
  • The generator’s native hot module reloading (HMR) repaints the preview in real time without restarting the server or creating Git commits.

3. Heartbeat Keep-Alive & Session Preservation

  • Active previews send a heartbeat ping every 5 minutes (POST /api/sandbox/heartbeat).
  • Sessions are capped at 45 minutes to conserve quota.
  • Sitepins checks if preview browser tabs are actively open before sending deallocation beacons, ensuring tab reloads do not prematurely terminate live sessions.

Organization Sandbox Credentials

To enable live preview in your organization:

  1. Navigate to Organization Settings β†’ Sandbox Preview.
  2. Input your Vercel Access Token and Project details.
  3. Sitepins encrypts the token at rest using AES-256-GCM via SANDBOX_ENCRYPTION_KEY.
Last updated on