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
.("user:123");
// Expose records matching a pattern
.(/^product:\d+$/);
// With access control
.(/^private:.+$/, async (, ) => {
const = await ..();
return !!?.userId;
});
Server: writable records
// Allow clients to update records
.(/^cursor:user:\d+$/);
// With write permission check
.(/^profile:user:\d+$/, async (, ) => {
const = await ..();
return ?.userId === .(":").();
});
Server: update records
// Update a record (broadcasts to all subscribers)
await .("user:123", {
: "Alice",
: "active",
});
Client: subscribe
// Full mode (default)
const { , , } = await .(
"user:123",
() => {
.(`Received update for ${.}:`, .);
}
);
// Patch mode (more efficient for large or frequently changing records)
import { } from "@mesh-kit/client";
let = {};
await .(
"product:456",
() => {
if (.) {
(, .);
} else {
// The full update is received if the client falls out of sync
= .;
}
},
{ : "patch" }
);
Client: update records
// Update a writable record
const = await .("cursor:user:123", {
: 100,
: 250,
: .(),
});
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