Records
Records are versioned JSON documents stored in Redis. Clients can subscribe to them and receive real-time updates.
Each record has:
- A unique ID
- A current value
- A version number
- Optional patches for efficient updates
Server: expose records
// Expose a specific record
server.exposeRecord("user:123");
// Expose records matching a pattern
server.exposeRecord(/^product:\d+$/);
// With access control
server.exposeRecord(/^private:.+$/, async (conn, id) => {
const meta = await server.connectionManager.getMetadata(conn);
return !!meta?.userId;
});
Server: writable records
// Allow clients to update records
server.exposeWritableRecord(/^cursor:user:\d+$/);
// With write permission check
server.exposeWritableRecord(/^profile:user:\d+$/, async (conn, id) => {
const meta = await server.connectionManager.getMetadata(conn);
return meta?.userId === id.split(":").pop();
});
Server: update records
// Update a record (broadcasts to all subscribers)
await server.publishRecordUpdate("user:123", {
name: "Alice",
status: "active",
});
Client: subscribe
// Full mode (default)
const { success, record, version } = await client.subscribeRecord(
"user:123",
(update) => {
console.log(`Received update for ${update.recordId}:`, update.full);
}
);
// Patch mode (more efficient for large or frequently changing records)
import { applyPatch } from "@mesh-kit/core/client";
let productData = {};
await client.subscribeRecord(
"product:456",
(update) => {
if (update.patch) {
applyPatch(productData, update.patch);
} else {
// The full update is received if the client falls out of sync
productData = update.full;
}
},
{ mode: "patch" }
);
Client: update records
// Update a writable record
const success = await client.publishRecordUpdate("cursor:user:123", {
x: 100,
y: 250,
timestamp: Date.now(),
});
Modes
Clients choose how to receive updates:
- Full mode: entire value sent every time
- Patch mode: only JSON diffs are sent (smaller, more efficient)
⚠️
Patch mode only works for objects and arrays—not primitive values.
Versioning
Mesh tracks versions automatically. If a client misses an update or falls out of sync, it receives a full resync automatically.
Features
- Distributed state synchronization via Redis
- Automatic versioning and conflict resolution
- Efficient updates with JSON patches
- Access control via guards
- Client-side write capability (optional)
- Automatic resync after disconnection
- No client-side diffing required
- Optional persistence to durable storage
Use cases
Records can be used for both ephemeral and persistent data:
Ephemeral data (in-memory only):
- Collaborative editing (cursors, selections)
- Temporary game state (player positions)
- Form state sync
- Real-time indicators
Persistent data (with persistence enabled):
- User profiles and settings
- Game save states
- Document content
- Application configuration
See Server → Records and Client → Records for more details on writable updates, guards, subscriptions, and reconnect handling.
Last updated on