Convex Backend Architecture
CodeJam is built on Convex, a real-time backend-as-a-service that provides:- Type-safe API: Full TypeScript support from backend to frontend
- Real-time reactivity: Automatic UI updates when data changes
- Serverless functions: Queries, mutations, and actions run on-demand
- Built-in auth: Integrated authentication with multiple providers
Function Types
Convex provides three types of backend functions:Queries
Queries are read-only operations that fetch data. They:- Cannot modify the database
- Are automatically cached and reactive
- Trigger React component re-renders when data changes
- Run transactionally with consistent snapshot reads
Mutations
Mutations are write operations that modify data. They:- Can read and write to the database
- Run transactionally (all-or-nothing)
- Are not cached
- Trigger query invalidation to update subscribed components
Actions
Actions are non-transactional operations for external API calls, long-running tasks, or operations requiring third-party services. They:- Can call external APIs
- Can call queries and mutations
- Do not run transactionally
- Have longer timeout limits
Using the API from React
Queries with useQuery
Queries are reactive - your component automatically re-renders when data changes:
Mutations with useMutation
Mutations are called imperatively:
Real-time Reactivity
One of Convex’s most powerful features is automatic real-time updates:- Subscribe once: Use
useQueryto subscribe to data - Automatic updates: When data changes (via any mutation), all subscribed components re-render
- No manual refetching: No need for
refetch()or cache invalidation - Optimistic updates: Mutations can return immediately while processing
Authentication Pattern
All authenticated endpoints use thegetAuthUserId pattern:
Error Handling
Convex functions can throw errors that are automatically propagated to the client:Next Steps
- Authentication - Learn about the auth system
- Users API - User queries and mutations
- Games API - Game metadata and stats
- Battles API - Multiplayer battle system
- Social API - Friends and notifications