examples/with-temporal/README.md
This is a starter project for creating resilient Next.js applications with Temporal. Whenever our API routes need to do any of the following, we can greatly increase our code's fault tolerance by using Temporal:
The starter project has this logic flow:
localhost:3000/api/orderspages/api/orders/index.ts:
orders task queueReserving 2 of item B102
Charging user 123 for 2 of item B102
Here is the Temporal code:
temporal/src/workflows/order.tstemporal/src/activities/{payment|inventory}.tsThere are three parts of this starter project that are left unimplemented:
pages/api/orders/index.tstemporal/src/activities/inventory.tstemporal/src/activities/payment.tsThe Next.js server can be deployed using Vercel:
One or more instances of the worker (temporal/src/worker/) can be deployed to a PaaS (each worker is a long-running Node process, so it can't run on a FaaS/serverless platform).
Temporal Server is a cluster of internal services, a database of record, and a search database. It can be run locally with Docker Compose and deployed with a container orchestration service like Kubernetes or ECS.
Execute create-next-app with npm, Yarn, or pnpm to bootstrap the example:
npx create-next-app --example with-temporal next-temporal-app
yarn create next-app --example with-temporal next-temporal-app
pnpm create next-app --example with-temporal next-temporal-app
The Temporal Node SDK requires Node >= 14, node-gyp, and Temporal Server. Once you have everything installed, you can develop locally with the below commands in four different shells:
In the Temporal Server docker directory:
docker compose up
In the next-temporal-app/ directory:
npm run dev
npm run build-worker.watch
npm run start-worker