|
| 1 | +## Trigger.dev + Next.js Realtime CSV Importer example |
| 2 | + |
| 3 | +This demo is a full stack example that demonstrates how to use Trigger.dev Realtime to a CSV Uploader with live progress updates. The frontend is a Next.js app that allows users to upload a CSV file, which is then processed in the background using Trigger.dev tasks. The progress of the task is streamed back to the frontend in real-time using Trigger.dev Realtime. |
| 4 | + |
| 5 | +- A [Next.js](https://nextjs.org/) app with [Trigger.dev](https://trigger.dev/) for the background tasks. |
| 6 | +- [UploadThing](https://uploadthing.com/) to handle CSV file uploads |
| 7 | +- Trigger.dev [Realtime](https://trigger.dev/launchweek/0/realtime) to stream updates to the frontend. |
| 8 | + |
| 9 | +## Getting started |
| 10 | + |
| 11 | +1. After cloning the repo, run `npm install` to install the dependencies. |
| 12 | +2. Copy the `.env.example` file to `.env` and fill in the required environment variables. If you haven't already, sign up for a free Trigger.dev account [here](https://cloud.trigger.dev/login) and create a new project. |
| 13 | +3. Copy the project ref from the Trigger.dev dashboard and and add it to the `trigger.config.ts` file. |
| 14 | +4. Run the Next.js server with `npm run dev`. |
| 15 | +5. In a separate terminal, run the Trigger.dev dev CLI command with with `npm exec trigger dev` (it may ask you to authorize the CLI if you haven't already). |
| 16 | + |
| 17 | +Now you should be able to visit `http://localhost:3000` and see the app running. Upload a CSV file and watch the progress bar update in real-time! |
| 18 | + |
| 19 | +## Relevant code |
| 20 | + |
| 21 | +- View the Trigger.dev task code in the [src/trigger/csv.ts](src/trigger/csv.ts) file. |
| 22 | +- The parent task `csvValidator` downloads the CSV file, parses it, and then splits the rows into multiple batches. It then does a `batch.triggerAndWait` to distribute the work the `handleCSVRow` task. |
| 23 | +- The `handleCSVRow` task "simulates" checking the row for a valid email address and then updates the progress of the parent task using `metadata.parent`. See the [Trigger.dev docs](https://trigger.dev/docs/runs/metadata#parent-and-root-updates) for more information on how to use the `metadata.parent` object. |
| 24 | +- The `useRealtimeCSVValidator` hook in the [src/hooks/useRealtimeCSVValidator.ts](src/hooks/useRealtimeCSVValidator.ts) file handles the call to `useRealtimeRun` to get the progress of the parent task. |
| 25 | +- The `CSVProcessor` component in the [src/components/CSVProcessor.tsx](src/components/CSVProcessor.tsx) file handles the file upload and displays the progress bar, and uses the `useRealtimeCSVValidator` hook to get the progress updates. |
| 26 | + |
| 27 | +## Learn More |
| 28 | + |
| 29 | +To learn more about Trigger.dev Realtime, take a look at the following resources: |
| 30 | + |
| 31 | +- [Trigger.dev Documentation](https://trigger.dev/docs) - learn about Trigger.dev and its features. |
| 32 | +- [Metadata docs](https://trigger.dev/docs/runs/metadata) - learn about the Run Metadata feature of Trigger.dev. |
| 33 | +- [Batch Trigger docs](https://trigger.dev/docs/triggering) - learn about the Batch Trigger feature of Trigger.dev. |
| 34 | +- [Realtime docs](https://trigger.dev/docs/realtime) - learn about the Realtime feature of Trigger.dev. |
| 35 | +- [React hooks](https://trigger.dev/docs/frontend/react-hooks) - learn about the React hooks provided by Trigger.dev. |
0 commit comments