Overview
Liveblocks provides:- Real-time synchronization: Multiple users can edit the same LaTeX document simultaneously
- User presence: See who’s currently viewing or editing a document
- Collaborative cursors: View other users’ cursor positions in real-time
- Conflict-free merges: Using Yjs CRDT for automatic conflict resolution
- Persistent storage: Document state is stored and synced across sessions
Liveblocks is a hosted service. You don’t need to manage any database infrastructure yourself.
Prerequisites
- A Liveblocks account (sign up at liveblocks.io)
- A project created in your Liveblocks dashboard
Creating a Liveblocks project
- Sign in to your Liveblocks dashboard
- Click Create project
- Choose a name for your project (e.g., “Typeset Self-Hosted”)
- Select your preferred region for data storage
- Click Create
Obtaining your secret key
- In your project dashboard, navigate to API Keys
- Copy your Secret key (starts with
sk_) - Add it to your
.env.localfile:
Server-side configuration
Typeset initializes the Liveblocks client on the server:lib/liveblocks.ts
lib/liveblocks.ts:3-5 for the implementation.
Authentication integration
Liveblocks uses Clerk authentication to identify users. When a user joins a collaboration session, Typeset creates a Liveblocks user session:app/api/liveblocks-auth/route.ts
app/api/liveblocks-auth/route.ts:27-50 for the complete implementation.
User information structure
Each collaborative user has the following information:liveblocks.config.ts
liveblocks.config.ts:1-9.
User colors
Each user is assigned a random color from a predefined palette for their cursor and presence indicator:#E74C3C- Red#8E44AD- Purple#6C5CE7- Indigo#2E86AB- Blue#059669- Green#D97706- Amber- And more…
Client-side setup
Typeset uses the following Liveblocks packages on the client:@liveblocks/client- Core client library@liveblocks/react- React hooks and components@liveblocks/yjs- Yjs CRDT integration
Yjs integration
Typeset uses Yjs for conflict-free collaborative text editing. The integration with CodeMirror enables real-time multi-user editing:- Sync document state across all connected users
- Automatically resolve editing conflicts
- Maintain document consistency
- Preserve undo/redo history per user
Room-based collaboration
Liveblocks uses “rooms” to organize collaborative sessions. In Typeset:- Each LaTeX project is a separate Liveblocks room
- Room IDs are based on project identifiers
- Users join/leave rooms as they navigate projects
- Room state persists even when no users are connected
Room data is automatically persisted by Liveblocks. You don’t need to manually save document state.
Configuring room permissions
In your Liveblocks dashboard:- Navigate to Permissions
- Configure who can access rooms:
- Public rooms: Anyone with the link can join
- Private rooms: Only authenticated users can join
- Custom permissions: Define fine-grained access rules
Monitoring and usage
Liveblocks dashboard provides:- Active connections: Real-time view of connected users
- Room analytics: Usage statistics per room
- API usage: Track your API request volume
- Storage metrics: Monitor stored data size
Rate limits
Be aware of Liveblocks rate limits based on your plan:- Free tier: Limited connections and API requests
- Paid tiers: Higher limits and enterprise options
Data persistence
Liveblocks automatically handles data persistence:- Document changes are saved in real-time
- State is preserved when all users disconnect
- Historical versions can be accessed (plan-dependent)
- Data is encrypted at rest and in transit
Testing collaboration
After configuration:- Start your development server:
npm run dev - Sign in to your Typeset instance
- Create a new project
- Open the same project in multiple browser tabs or windows
- Make edits in one tab and verify they appear in others
- Check that user cursors and presence indicators work
Production considerations
Webhooks
Liveblocks supports webhooks for events like:- User joins/leaves a room
- Room is created or deleted
- Storage is updated
Data regions
Choose a Liveblocks data region close to your users for optimal performance:- US East (Virginia)
- Europe (Frankfurt)
- Asia Pacific (Singapore)
Troubleshooting
Users can’t connect to rooms
Verify:LIVEBLOCKS_SECRET_KEYis correctly set- Users are authenticated via Clerk
- Room permissions allow authenticated access
Changes not syncing
Check:- Network connectivity between client and Liveblocks servers
- Browser console for WebSocket errors
- Liveblocks dashboard for service status
Rate limit errors
If you’re hitting rate limits:- Upgrade your Liveblocks plan
- Optimize connection patterns
- Implement connection pooling
Alternative: Self-hosted collaboration
While Liveblocks is a hosted service, you could replace it with a self-hosted alternative like y-websocket or y-redis for complete data sovereignty.This would require significant code changes to replace the Liveblocks SDK with a custom Yjs provider.
Next steps
Environment variables
Review all required configuration
Authentication
Learn how Clerk integrates with Liveblocks