Installation
Importing
If you are using React, use the
convex/react module instead for better integration with React’s rendering and state management.ConvexClient
A WebSocket-based client that subscribes to queries and executes mutations and actions. Provides reactive updates when query results change.Basic usage
Constructor options
disabled- Disables subscriptions (useful for SSR)unsavedChangesWarning- Prompts users about unsaved changes before leaving
Subscribing to queries
TheonUpdate method subscribes to a query and calls a callback when results change:
Executing mutations
Executing actions
One-time queries
Authentication
Set authentication with an async token fetcher:Connection state
Paginated queries (experimental)
Cleanup
Always close the client when done:ConvexHttpClient
An HTTP-based client for executing queries, mutations, and actions without maintaining a WebSocket connection. Suitable for server-side code or applications that don’t need real-time updates.Basic usage
Constructor options
Executing queries
Executing mutations
Mutations are queued by default to ensure ordered execution:Executing actions
Authentication
Consistent queries (experimental)
Execute multiple queries at the same timestamp:Choosing a client
Use
ConvexClient when:
- Building a browser application without React
- You need real-time updates
- You want to subscribe to query changes
ConvexHttpClient when:
- Running in serverless functions or edge workers
- Making one-off requests
- You don’t need real-time updates
- Minimizing connection overhead is important
Error handling
Type safety
All clients use generated TypeScript types:BaseConvexClient
For advanced use cases, you can extendBaseConvexClient to build custom clients:
Best practices
- Store deployment URLs in environment variables
- Always close clients when done to prevent memory leaks
- Use
ConvexClientfor browser apps that need reactivity - Use
ConvexHttpClientfor server-side code - Handle errors appropriately for mutations and actions
- For React applications, use
convex/reactinstead - Implement connection state UI for better user experience