1. Overview
This system converts any static website or game into a
fully dynamic, realtime‑synchronized environment
where every UI element or game object is bound to a Supabase variable.
The frontend is hosted on GitHub Pages (or any static host).
All state is stored, updated, and broadcast through
Supabase Postgres + Realtime + Edge Functions.
Implementations must ensure:
- All UI/game elements are data‑driven
- All state changes propagate instantly
- All clients stay synchronized
- All updates respect Row Level Security (RLS)
- No secrets are exposed in the frontend
2. High‑level architecture
- Frontend: Static HTML/JS/CSS on GitHub Pages
- Backend: Supabase (Postgres, Realtime, Edge Functions)
- Transport: WebSockets via Supabase Realtime
- State: Tables
ui_elements, game_objects, events
Intent: This spec is meant to be
agent‑implementable. Any GitHub Agent, Supabase Edge Agent,
or human developer can follow it to wire up a live system without needing
additional context from the original author.
3. Data model
3.1 Table: ui_elements
Stores all website/game UI elements.
create table if not exists public.ui_elements (
id text primary key,
type text not null,
value jsonb not null default '{}'::jsonb,
updated_at timestamptz not null default now()
);
3.2 Table: game_objects
Stores all game world objects.
create table if not exists public.game_objects (
id text primary key,
kind text not null,
state jsonb not null default '{}'::jsonb,
updated_at timestamptz not null default now()
);
3.3 Table: events
Used for ephemeral realtime actions (movement, chat, triggers).
create table if not exists public.events (
id bigserial primary key,
channel text not null,
payload jsonb not null,
created_at timestamptz not null default now()
);
3.4 Suggested seed data
insert into public.ui_elements (id, type, value)
values
('hero-title', 'text', jsonb_build_object('text', 'Hello, realtime world')),
('hero-subtitle', 'text', jsonb_build_object('text', 'This text is coming from Supabase.'));
insert into public.game_objects (id, kind, state)
values
('player-1', 'player', jsonb_build_object('x', 0, 'y', 0, 'hp', 100));
Agent note: You may extend the schema with additional
columns or tables as needed, but ui_elements,
game_objects, and events must remain intact
and compatible with this contract.
4. Realtime subscriptions
4.1 UI sync
Subscribe to all changes in ui_elements:
const uiChannel = supabase
.channel('ui-sync')
.on(
'postgres_changes',
{ event: '*', schema: 'public', table: 'ui_elements' },
payload => {
const row = payload.new;
handleUiUpdate(row);
}
)
.subscribe();
function handleUiUpdate(row) {
const el = document.getElementById(row.id);
if (!el) return;
const value = row.value || {};
if (row.type === 'text' && typeof value.text === 'string') {
el.textContent = value.text;
}
if (value.style && typeof value.style === 'object') {
Object.entries(value.style).forEach(([k, v]) => {
el.style[k] = v;
});
}
}
4.2 Game object sync
Subscribe to all changes in game_objects:
const gameChannel = supabase
.channel('game-sync')
.on(
'postgres_changes',
{ event: '*', schema: 'public', table: 'game_objects' },
payload => {
const row = payload.new;
handleGameUpdate(row);
}
)
.subscribe();
function handleGameUpdate(row) {
// Implement your own renderGameObject(id, kind, state)
renderGameObject(row.id, row.kind, row.state || {});
}
4.3 Event stream
Subscribe to events for ephemeral actions:
const eventsChannel = supabase
.channel('events')
.on(
'postgres_changes',
{ event: 'INSERT', schema: 'public', table: 'events' },
payload => {
const row = payload.new;
handleEvent(row.channel, row.payload);
}
)
.subscribe();
5. Frontend binding contract
- Each DOM element or game object must have a stable ID.
- That ID maps directly to a row in Supabase.
- The frontend must render based on Supabase state, not hardcoded values.
5.1 UI element rendering
function renderElement(id, type, value) {
const el = document.getElementById(id);
if (!el) return;
if (type === 'text' && typeof value.text === 'string') {
el.textContent = value.text;
}
if (value.style && typeof value.style === 'object') {
Object.entries(value.style).forEach(([k, v]) => {
el.style[k] = v;
});
}
}
5.2 Game object rendering
function renderGameObject(id, kind, state) {
// Example: integrate with your canvas, WebGL, or engine layer
// This function is intentionally left abstract for implementers.
}
6. Update protocol
6.1 UI updates
async function updateUiElement(elementId, value) {
const { error } = await supabase
.from('ui_elements')
.update({ value })
.eq('id', elementId);
if (error) {
console.error('Failed to update UI element', error);
}
}
6.2 Game state updates
async function updateGameObject(objectId, state) {
const { error } = await supabase
.from('game_objects')
.update({ state })
.eq('id', objectId);
if (error) {
console.error('Failed to update game object', error);
}
}
6.3 Event emission
async function emitEvent(channel, payload) {
const { error } = await supabase
.from('events')
.insert({ channel, payload });
if (error) {
console.error('Failed to emit event', error);
}
}
7. Security model
7.1 RLS requirements
Row Level Security must be enabled and enforced.
alter table public.ui_elements enable row level security;
alter table public.game_objects enable row level security;
alter table public.events enable row level security;
Example basic policies (to be adapted per project):
-- Example: allow read for all authenticated users
create policy "ui_elements_select"
on public.ui_elements
for select
using (true);
-- Example: restrict updates to editor role
create policy "ui_elements_update_editors"
on public.ui_elements
for update
using (auth.role() = 'editor');
-- Example: players can only update their own game object
create policy "game_objects_update_self"
on public.game_objects
for update
using (state ? 'owner_id' AND state->>'owner_id' = auth.uid()::text);
7.2 No secrets in frontend
- Use the public anon key in the frontend.
- Use the service role key only in Edge Functions.
- Never embed secrets in GitHub Pages or any static asset.
8. Edge functions
Recommended functions (names are suggestions, not strict):
update-ui — validates and writes UI updates
update-game-object — validates and writes game state updates
emit-event — validates and inserts events
Agent note: If your threat model is strict, route all
writes through Edge Functions and keep the frontend limited to reads +
signed, validated mutations.
9. Agent task list
9.1 Required tasks
- Create all tables:
ui_elements, game_objects, events
- Enable and configure RLS policies
- Implement Edge Functions (if used) for validated writes
- Implement realtime subscriptions for UI, game objects, and events
- Implement
renderElement and renderGameObject
- Implement update helpers:
updateUiElement, updateGameObject, emitEvent
- Ensure idempotent, stateless rendering logic
- Ensure no secrets are present in the frontend code
9.2 Optional tasks
- Add optimistic UI updates and reconciliation
- Add offline mode with local cache + sync
- Add versioning for layouts and game states
- Add a visual editor that writes directly to
ui_elements
10. Completion criteria
- All UI elements update in realtime across clients
- All game objects sync across clients
- Events broadcast instantly via
events table
- No secrets exist in the frontend
- RLS is enforced and tested
- GitHub Pages build is fully functional
- Supabase schema and policies are deployed
- Agents can modify any element or object by editing Supabase rows
Execution rule: Treat this spec as authoritative.
Do not remove required tables or fields. Do not break realtime sync
or RLS. Extensions are allowed as long as they remain compatible.