Real-time collaborative list

Build a list that several users subscribe to and see each other's changes in real time.

Every client subscribes to the same data and sees changes as they happen. This recipe shows the pattern end-to-end.

Schema

A shared project with collaboratively-edited tasks.

schema.ts
import { schema as s } from "jazz-tools";
​
export const schema = s.defineSchema({
projectMembers: s.table(
{ projectId: s.uuid(), user_id: s.uuid() },
{
project: s.rel("projects", "projectId"),
},
),
projects: s.table(
{ name: s.string() },
{
members: s.reverse("projectMembers", "project"),
tasks: s.reverse("tasks", "project"),
},
),
tasks: s.table(
{ title: s.string(), done: s.boolean(), assignee_id: s.uuid().optional(), projectId: s.uuid() },
{
project: s.rel("projects", "projectId"),
},
),
});

Permissions

Project members can read, add and edit tasks. The creator can manage membership. Tasks inherit access from their project via allowedTo.

The creator and members can read a project; only the creator can update it. Only the creator can add members. A membership row is readable by the project's creator and by the member it names. There are no delete rules, so projects, tasks and memberships can't be deleted.

schema.ts
const { definePermissions, exists, allowedTo, session, anyOf } = schema;
​
const createdByMe = { $createdBy: session.user };
​
export const permissions = definePermissions({
projectMembers: {
read: (projectMember) =>
anyOf(projectMember.project.where(createdByMe), { user_id: session.user }),
insert: (projectMember) => projectMember.project.where(createdByMe),
},
projects: {
read: (project) =>
anyOf(createdByMe, exists.projectMembers({ projectId: project.id, user_id: session.user })),
insert: true,
update: createdByMe,
},
tasks: {
read: (task) => allowedTo.read(task.project),
insert: (task) => allowedTo.read(task.project),
update: (task) => allowedTo.read(task.project),
},
});

See Declarative Permissions for exists, anyOf and allowedTo.

Subscribing to shared data

When multiple clients subscribe to the same query, they all see each other's changes in real-time.

ProjectTasks.tsx
export function ProjectTasks({ projectId }: { projectId: string }) {
const {
data: tasks,
isLoading,
error,
db,
} = useAll(app.tasks.where({ projectId, done: false }).orderBy("$createdAt", "desc"));
​
function addTask(title: string) {
db.insert(app.tasks, { title, done: false, projectId });
}
​
function completeTask(taskId: string) {
db.update(app.tasks, taskId, { done: true });
}
​
if (isLoading) return <p>Loading…</p>;
if (error) return <p>Something went wrong!</p>;
​
return (
<ul>
{tasks.map((task) => (
<li key={task.id}>
<button onClick={() => completeTask(task.id)}>Done</button>
{task.title}
</li>
))}
</ul>
);
}

When Alice inserts a task it appears in her UI instantly, syncs to the server, and the server pushes it to Bob — whose useAll subscription re-renders automatically.

When two users edit the same row concurrently, Jazz merges per column. Each column resolves independently, so if Alice updates title while Bob updates done, both changes are preserved. If they both update title, the edit the server receives last wins and the other is kept in the row's history.

For most applications this is the right default. See How sync works for more detail.