|
| 1 | +--- |
| 2 | +title: "Next.js Realtime CSV Importer" |
| 3 | +sidebarTitle: "Realtime CSV Importer" |
| 4 | +description: "This example Next.js project demonstrates how to use Trigger.dev Realtime to build a CSV Uploader with progress updates streamed to the frontend." |
| 5 | +--- |
| 6 | + |
| 7 | +import RealtimeLearnMore from "/snippets/realtime-learn-more.mdx"; |
| 8 | + |
| 9 | +## Overview |
| 10 | + |
| 11 | +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. |
| 12 | + |
| 13 | +- A [Next.js](https://nextjs.org/) app with [Trigger.dev](https://trigger.dev/) for the background tasks. |
| 14 | +- [UploadThing](https://uploadthing.com/) to handle CSV file uploads |
| 15 | +- Trigger.dev [Realtime](https://trigger.dev/launchweek/0/realtime) to stream updates to the frontend. |
| 16 | + |
| 17 | +## GitHub repo |
| 18 | + |
| 19 | +<Card |
| 20 | + title="View the Realtime CSV Importer repo" |
| 21 | + icon="GitHub" |
| 22 | + href="https://github.com/triggerdotdev/examples/blob/main/realtime-csv-importer/README.md" |
| 23 | +> |
| 24 | + Click here to view the full code for this project in our examples repository on GitHub. You can |
| 25 | + fork it and use it as a starting point for your own project. |
| 26 | +</Card> |
| 27 | + |
| 28 | +## Video |
| 29 | + |
| 30 | +<video |
| 31 | + controls |
| 32 | + className="w-full aspect-video" |
| 33 | + src="https://github.com/user-attachments/assets/25160343-6663-452c-8a27-819c3fd7b8df" |
| 34 | +></video> |
| 35 | + |
| 36 | +## Relevant code |
| 37 | + |
| 38 | +- View the Trigger.dev task code in the [src/trigger/csv.ts](https://github.com/triggerdotdev/examples/blob/main/realtime-csv-importer/src/trigger/csv.ts) file. |
| 39 | +- 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. |
| 40 | +- 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](/runs/metadata#parent-and-root-updates) for more information on how to use the `metadata.parent` object. |
| 41 | +- The `useRealtimeCSVValidator` hook in the [src/hooks/useRealtimeCSVValidator.ts](https://github.com/triggerdotdev/examples/blob/main/realtime-csv-importer/src/hooks/useRealtimeCSVValidator.ts) file handles the call to `useRealtimeRun` to get the progress of the parent task. |
| 42 | +- The `CSVProcessor` component in the [src/components/CSVProcessor.tsx](https://github.com/triggerdotdev/examples/blob/main/realtime-csv-importer/src/components/CSVProcessor.tsx) file handles the file upload and displays the progress bar, and uses the `useRealtimeCSVValidator` hook to get the progress updates. |
| 43 | + |
| 44 | +<RealtimeLearnMore /> |
0 commit comments