How real-time collaboration works
Sanity’s real-time collaboration is built on three core concepts:- Presence: See which users are viewing or editing documents
- Live mutations: Changes sync across all connected clients
- Optimistic updates: UI updates immediately while syncing in background
Architecture overview
The collaboration system uses:- WebSocket connections for real-time event streaming
- Mutation system for applying changes
- Document store for managing subscriptions
- Event system for change propagation
Presence system
Presence shows who is currently viewing or editing a document.How presence works
- Each user’s session broadcasts their location and activity
- Presence is shown in the document header and on specific fields
- Presence indicators update in real-time as users navigate
Viewing presence in the UI
The studio automatically shows:- User avatars in the document toolbar for all viewers
- Field-level indicators when someone is editing a specific field
- Typing indicators for text fields being actively edited
Live mutations
Mutations are changes to documents that sync in real-time.Mutation flow
The mutator package
The@sanity/mutator package handles mutation logic:
- Applies patches to documents
- Handles conflicts when multiple users edit the same field
- Validates mutations before applying
- Maintains document integrity during concurrent edits
Conflict resolution
When multiple users edit the same field:- Last write wins: The most recent change takes precedence
- Field-level granularity: Different fields can be edited simultaneously
- Optimistic UI: Each user sees their changes immediately
- Server reconciliation: The server determines the final state
Sanity uses operational transformation principles to handle concurrent edits gracefully.
Real-time updates in custom components
You can subscribe to document changes in your components:Document store integration
The document store manages real-time subscriptions:Enabling comments
Sanity supports inline comments for collaboration:- Users can leave feedback on specific fields
- Comments appear in context
- Thread discussions in real-time
- Resolve comments when addressed
Collaborative workflows
Draft and published states
Sanity maintains separate draft and published versions:- Draft: Working copy with
drafts.prefix - Published: Live version without prefix
- Collaboration happens on drafts: Multiple users edit the draft
- Publishing is atomic: Draft replaces published in one operation
Version history
All changes are tracked:- View document history in the studio
- Restore previous versions
- Compare changes between versions
- See who made each change
Optimistic updates
The studio uses optimistic updates for instant feedback:Network handling
The studio gracefully handles network issues:- Offline editing: Changes queue locally
- Reconnection: Queued changes sync when online
- Conflict resolution: Server reconciles conflicting changes
- Status indicators: Shows connection state in UI
Rate limiting and throttling
To ensure performance:- Debounced updates: Rapid changes are batched
- Throttled presence: Presence updates are rate-limited
- Efficient subscriptions: Only active documents are monitored
Best practices for collaboration
Design for concurrent editing
Design for concurrent editing
Structure your schema to minimize conflicts:
- Use granular fields instead of large text blocks
- Separate frequently edited fields
- Consider using arrays for multi-user content
Communicate with your team
Communicate with your team
- Enable comments for async feedback
- Use presence indicators to avoid conflicts
- Establish workflows for content approval
Test with multiple users
Test with multiple users
- Open multiple browser windows
- Simulate concurrent edits
- Verify changes sync correctly
Monitoring collaboration
You can monitor collaboration activity:Disabling real-time features
If needed, you can disable certain features:Real-time sync cannot be disabled as it’s core to Sanity’s architecture.
Next steps
- Learn about document actions for custom workflows
- Explore querying with GROQ for real-time listeners
- See customizing the studio for collaboration UI