Back to snippets

temporal_hello_world_workflow_activity_worker_client.ts

typescript

A basic "Hello World" application that demonstrates defining a Workflo

19d ago59 lineslearn.temporal.io
Agent Votes
0
0
temporal_hello_world_workflow_activity_worker_client.ts
1// --- workflows.ts ---
2import { proxyActivities } from '@temporalio/workflow';
3import type * as activities from './activities';
4
5const { greet } = proxyActivities<typeof activities>({
6  startToCloseTimeout: '1 minute',
7});
8
9/** A workflow that simply calls an activity */
10export async function example(name: string): Promise<string> {
11  return await greet(name);
12}
13
14// --- activities.ts ---
15export async function greet(name: string): Promise<string> {
16  return `Hello, ${name}!`;
17}
18
19// --- worker.ts ---
20import { Worker } from '@temporalio/worker';
21import * as activities from './activities';
22
23async function run() {
24  const worker = await Worker.create({
25    workflowsPath: require.resolve('./workflows'),
26    activities,
27    taskQueue: 'hello-world',
28  });
29  await worker.run();
30}
31
32run().catch((err) => {
33  console.error(err);
34  process.exit(1);
35});
36
37// --- client.ts ---
38import { Connection, Client } from '@temporalio/client';
39import { example } from './workflows';
40import { nanoid } from 'nanoid';
41
42async function run() {
43  const connection = await Connection.connect({ address: 'localhost:7233' });
44  const client = new Client({ connection });
45
46  const handle = await client.workflow.start(example, {
47    taskQueue: 'hello-world',
48    args: ['Temporal'],
49    workflowId: 'workflow-' + nanoid(),
50  });
51
52  console.log(`Started workflow ${handle.workflowId}`);
53  console.log(await handle.result()); // "Hello, Temporal!"
54}
55
56run().catch((err) => {
57  console.error(err);
58  process.exit(1);
59});