This example demonstrates using absurder-sql in a Vite development environment with comprehensive multi-tab coordination.
# Install dependencies
npm install
# Build the WASM package (from repo root)
cd ../..
wasm-pack build --target web --out-dir pkg
cd examples/vite-app
# Start dev server
npm run dev- [✓] Hot module replacement with Vite
- [✓] SQLite database in the browser
- [✓] IndexedDB persistence
- [✓] Multi-tab leader election
- [✓] Automatic write coordination
- [✓] Real-time sync across tabs
- [✓] Leader/follower badge display
- [✓] Automatic UI updates on tab status change
- [✓] Write Queuing: Queue writes from any tab
- [✓] Optimistic Updates: Track pending writes
- [✓] Coordination Metrics: Monitor performance
- Open http://localhost:5173 in your browser
- Open the same URL in additional tabs
- Observe the leader badge ([LEADER] or [FOLLOWER])
- Try clicking "Run Test" in different tabs
- See how only the leader tab can write
- Click "Request Leadership" to become leader
- Watch how data changes sync across all tabs
- Leader Election: First tab becomes leader automatically
- Write Guard: Only leader can execute INSERT/UPDATE/DELETE
- Auto-Sync: Changes automatically sync to IndexedDB
- Notifications: BroadcastChannel notifies other tabs of changes
- Failover: When leader tab closes, another tab becomes leader
The app uses MultiTabDatabase wrapper for simplified multi-tab handling:
import { MultiTabDatabase } from '../multi-tab-wrapper.js';
const db = new MultiTabDatabase(Database, 'vite_example', {
autoSync: true, // Auto-sync after writes
waitForLeadership: false // Error if not leader
});
await db.init();
// Only leader can write
await db.write("INSERT INTO items VALUES (1, 'Item', 9.99)");
// OR use queueWrite from any tab
await db.queueWrite("INSERT INTO items VALUES (1, 'Item', 9.99)");
// Leaders execute immediately, followers forward to leader
// Any tab can read
const result = await db.query("SELECT * FROM items");
// Listen for changes from other tabs
db.onRefresh(() => {
console.log('Data changed in another tab!');
});
// Advanced Features:
// Optimistic Updates
await db.enableOptimisticUpdates(true);
const writeId = await db.trackOptimisticWrite("INSERT ...");
const pending = await db.getPendingWritesCount();
// Coordination Metrics
await db.enableCoordinationMetrics(true);
await db.recordLeadershipChange(true);
const metrics = JSON.parse(await db.getCoordinationMetrics());npm run build
npm run previewThe build output in dist/ is ready to deploy to any static host.
- Multi-Tab Coordination Guide - Complete guide with all features
- Demo Guide - How to run interactive demos
- Main README - Project overview
This app is tested with Playwright. Run tests from repo root:
npm run test:e2e