Hello World Walkthrough in TypeScript
In this tutorial, we'll go over the different components that make up a Temporal project. All of the code on this page is included in our Hello World sample, which we set up in our Getting Started (we recommend following along on GitPod).
The SDK steers developers to write their Workflows and Activities in TypeScript but vanilla JS is also supported.
Activity
@temporalio/activity API reference
Activities are called from Workflows in order to run non-deterministic code.
Any async function can be used as an Activity as long as its parameters and return value are serializable. Activities run in the Node.js execution environment, meaning you can easily port over any existing code into an Activity and it should work.
src/activities.ts
Workflow
@temporalio/workflow API reference
In the TypeScript SDK, each Workflow execution is run in a separate V8 isolate context in order to provide a deterministic runtime.
A Workflow is also an async function, but it has access to special Workflow APIs like Signals, Queries, Timers, and Child Workflows.
The snippet below uses proxyActivities
to create a function that, when called, schedules an Activity in the system.
src/workflows.ts
Worker
@temporalio/worker API reference
The Worker hosts Workflows and Activities, connects to Temporal Server, and continually polls a Task Queue for Commands coming from Clients (see below). See the list of WorkerOptions for customizing Worker creation.
src/worker.ts
Client
@temporalio/client API reference
The WorkflowClient
class is used to interact with existing Workflows or to start new ones.
It can be used in any Node.js process (for example, an Express web server) and is separate from the Worker.
src/client.ts
Testing
There is no official test suite for Workflows and Activities yet.
- Since Activities are async functions, they should be testable as long as you avoid using Context or are able to mock it.
- You can test Workflows by running them with a WorkflowClient.
- Check the SDK's own tests for more examples.
Next Steps
You should now be familiar with the Hello World project, which is the main way we encourage scaffolding out new projects.
Two paths from here:
- Go Full Stack: Integrate the manually-run Temporal Client scripts you have into an Express.js app, or serverless function. Our Next.js Tutorial should help show you how to integrate with the frontend, and give indications on how to deploy.
- Learn More: Explore using Signals, Queries and Timers in our Subscription Workflow tutorial.