Convex Demos
repository·main·Indexed 43 days ago
https://github.com/get-convex/convex-demosA collection of demo applications showcasing features and integration patterns for the Convex platform, including argument validation, Clerk authentication, cron jobs, custom application errors, file storage, HTTP actions, and third-party API integrations with Dall-E and GIPHY.
What's inside Convex Demos
- Convex is a reactive backend-as-a-service (BaaS) designed for web developers. It allows you to store shared application state and synchronize it across all clients automatically. Developers interact with state using JavaScript or TypeScript functions, and Convex handles real-time updates, caching, and serverless scaling automatically.
Overview of File Storage with HTTP Actions
mainThis demo illustrates how to integrate Convex file storage with Convex Chat by using HTTP actions to allow image uploads. The workflow involves uploading images to Convex file storage and saving their corresponding storage IDs into themessagestable to enable image access within the chat interface.Overview of the File Storage Example App
mainThis application demonstrates how to integrate Convex file storage into a chat application to support image uploads. It extends the standard Convex chat tutorial by adding functionality to upload images, which are then stored in Convex file storage. The resulting storage IDs are persisted in themessagestable to allow for retrieval and display within the chat interface.Overview of the System Tables Example App
mainThe System Tables Example App demonstrates how to programmatically interact with Convex system tables to manage file storage and scheduled functions.
Key features include:
- File Storage Integration: An extension of the Convex chat tutorial that adds image uploading capabilities using Convex file storage.
- Scheduled Functions: The ability to schedule messages to be sent in the future.
- Admin Interface: Uses system tables to expose an interface for managing stored files and scheduled messages.
Users and Authentication Example App Overview
mainThis example demonstrates implementing user management and authentication within a chat application. It utilizes Auth0 for the authentication flow.
Key features include:
- User Persistence: Upon successful login, user information is automatically persisted to a
userstable. - Authenticated Messaging: Messages sent by users are programmatically associated with their specific user record.
- Session Management: Provides UI flows for both logging in and logging out.
- User Persistence: Upon successful login, user information is automatically persisted to a
Use Convex with Next.js App Router SSR
mainThis demo demonstrates how to integrate Convex with the Next.js App Router to achieve Server-Side Rendering (SSR). It specifically covers:
- Preloading data for Client Components: Using the Convex Next.js integration to fetch data on the server so it is available immediately when the client component hydrates.
- Server-side Data Operations: Performing queries and mutations directly from Server Actions or Route Handlers (API Routes) instead of relying solely on client-side hooks.
# To run this specific demo: npm install npm run devArgument Validation Example App Overview
mainThis demo application illustrates how to implement argument validation for Convex functions. It specifically demonstrates how to use a validator (located inconvex/messages.js) to ensure that the parameters passed toapi.messages.sendconform to the expected types before the function executes.Use TypeScript and Schemas in Convex
mainThis example demonstrates the standard pattern for building type-safe Convex applications:
- Convex Functions: Written in
.tsfiles within theconvex/directory. - React Components: Written in
.tsxfiles for frontend integration. - Data Modeling: A Convex schema is defined in
convex/schema.ts. This schema automatically generates TypeScript types specific to your application's data model, ensuring end-to-end type safety between your database and your functions/frontend.
- Convex Functions: Written in
Implement user sessions without authentication
mainThis pattern allows you to track per-tab or per-browser data in a database table without requiring a user to be logged in. It uses
convex-helpersto wrap Convex functions and custom React hooks to manage the session state on the client.To implement this pattern, you must:
- Wrap your application in a
SessionProviderinside yourConvexProvider. - Define backend functions using
queryWithSession(or the mutation/action equivalents). - Access the session ID via
ctx.session._idwithin your function handler. - Use
useSessionQuery(or the mutation/action equivalents) in your React components to interact with these functions.
// 1. Wrap your app <ConvexProvider client={convex}> <SessionProvider> <App /> </SessionProvider> </ConvexProvider>- Wrap your application in a
Call Convex functions using string paths (No Codegen)
mainIf you do not have access to the generatedapiobject or prefer not to use codegen, you can call Convex functions using string identifiers. The pattern follows the format"folder/file:exportName", which is the string equivalent of theapi.folder.file.exportNameobject path.Use the withZod helper for argument validation
mainThe
withZodhelper library allows you to validate both input and output arguments for Convex functions using Zod schemas. This provides a middleware-like pattern for ensuring type safety and data integrity at the function boundary. For a deep dive into the implementation pattern, refer to the official Convex blog post on wrappers as middleware.// Implementation details are located in ./convex/lib/withZod.tsUnderstand Custom Application Errors in Convex
mainThis application demonstrates how to implement application-level error handling in Convex. Instead of generic system errors, you can throw specific errors from your functions that the client can catch and react to.
In this specific demo, custom errors are thrown under the following conditions:
- Attempting to send a message that exceeds 50 characters in length.
- Attempting to list more than 20 messages.
For detailed implementation patterns, refer to the official Application Errors documentation.