@@ -156,8 +156,8 @@ This test will confirm that:
156156Create an Activity file (activity.ts):
157157
158158``` ts
159- export async function activity (name : string ): Promise <string > {
160- return ' Hello ' + name ;
159+ export async function greet (name : string ): Promise <string > {
160+ return ` Hello, ${ name }! ` ;
161161}
162162```
163163
@@ -169,15 +169,16 @@ Create a Workflow file (workflows.ts):
169169
170170``` ts
171171import { proxyActivities } from ' @temporalio/workflow' ;
172- import type * as activityTypes from ' ./activity' ;
172+ // Only import the activity types
173+ import type * as activities from ' ./activities' ;
173174
174- const { activity } = proxyActivities <typeof activityTypes >({
175- startToCloseTimeout: ' 10 seconds ' ,
175+ const { greet } = proxyActivities <typeof activities >({
176+ startToCloseTimeout: ' 1 minute ' ,
176177});
177178
178- export async function sayHelloWorkflow( name : string ) : Promise < string > {
179- const response = await activity (name );
180- return response ;
179+ /** A workflow that simply calls an activity */
180+ export async function example (name : string ) : Promise < string > {
181+ return await greet ( name ) ;
181182}
182183```
183184
@@ -191,25 +192,42 @@ If the application itself crashes, Temporal will automatically recreate its pre-
191192Create a Worker file (worker.ts):
192193
193194``` ts
194- import { Worker } from ' @temporalio/worker' ;
195- import * as activitiy from ' ./activity ' ;
195+ import { NativeConnection , Worker } from ' @temporalio/worker' ;
196+ import * as activities from ' ./activities ' ;
196197
197198async function run() {
198- // Step 1: Register Workflows and Activities with the Worker and connect to
199- // the Temporal server.
200- const worker = await Worker .create ({
201- workflowsPath: require .resolve (' ./workflows' ),
202- activities: activitiy ,
203- taskQueue: ' my-task-queue' ,
199+ // Step 1: Establish a connection with Temporal server.
200+ //
201+ // Worker code uses `@temporalio/worker.NativeConnection`.
202+ // (But in your application code it's `@temporalio/client.Connection`.)
203+ const connection = await NativeConnection .connect ({
204+ address: ' localhost:7233' ,
205+ // TLS and gRPC metadata configuration goes here.
204206 });
205- // Worker connects to localhost by default and uses console.error for logging.
206- // Customize the Worker by passing more options to create():
207- // https://typescript.temporal.io/api/classes/worker.Worker
208- // If you need to configure server connection parameters, see docs:
209- // https://docs.temporal.io/typescript/security#encryption-in-transit-with-mtls
210-
211- // Step 2: Start accepting tasks on the my-task-queue queue
212- await worker .run ();
207+ try {
208+ // Step 2: Register Workflows and Activities with the Worker.
209+ const worker = await Worker .create ({
210+ connection ,
211+ namespace: ' default' ,
212+ taskQueue: ' hello-world' ,
213+ // Workflows are registered using a path as they run in a separate JS context.
214+ workflowsPath: require .resolve (' ./workflows' ),
215+ activities ,
216+ });
217+
218+ // Step 3: Start accepting tasks on the `hello-world` queue
219+ //
220+ // The worker runs until it encounters an unexpected error or the process receives a shutdown signal registered on
221+ // the SDK Runtime object.
222+ //
223+ // By default, worker logs are written via the Runtime logger to STDERR at INFO level.
224+ //
225+ // See https://typescript.temporal.io/api/classes/worker.Runtime#install to customize these defaults.
226+ await worker .run ();
227+ } finally {
228+ // Close the connection once the worker has stopped
229+ await connection .close ();
230+ }
213231}
214232
215233run ().catch ((err ) => {
@@ -239,31 +257,32 @@ This final step will validate that everything is working correctly with your fil
239257Create a separate file called ` client.ts ` .
240258
241259``` ts
242- import { Connection , WorkflowClient } from ' @temporalio/client' ;
243- import { sayHelloWorkflow } from ' ./workflows' ;
260+ import { Client , Connection } from ' @temporalio/client' ;
261+ import { nanoid } from ' nanoid' ;
262+ import { example } from ' ./workflows' ;
244263
245264async function run() {
246- // Connect to the default Server location (localhost:7233)
247- const connection = await Connection .connect ();
265+ // Connect to the default Server location
266+ const connection = await Connection .connect ({ address: ' localhost:7233 ' } );
248267 // In production, pass options to configure TLS and other settings:
249268 // {
250- // address: 'foo.bar.tmprl.cloud',
251- // tls: {}
269+ // address: 'foo.bar.tmprl.cloud',
270+ // tls: {}
252271 // }
253272
254- const client = new WorkflowClient ({
273+ const client = new Client ({
255274 connection ,
256275 // namespace: 'foo.bar', // connects to 'default' namespace if not specified
257276 });
258277
259- const handle = await client .start (sayHelloWorkflow , {
278+ const handle = await client .workflow .start (example , {
279+ taskQueue: ' hello-world' ,
260280 // type inference works! args: [name: string]
261281 args: [' Temporal' ],
262- taskQueue: ' my-task-queue' ,
263- // in practice, use a meaningful business id, eg customerId or transactionId
264- workflowId: ' my-first-workflow' ,
282+ // in practice, use a meaningful business ID, like customerId or transactionId
283+ workflowId: ' workflow-' + nanoid (),
265284 });
266- console .log (' Started workflow ' + handle .workflowId );
285+ console .log (` Started workflow ${ handle .workflowId } ` );
267286
268287 // optional: wait for client result
269288 console .log (await handle .result ()); // Hello, Temporal!
0 commit comments