Skip to content

Commit 818c543

Browse files
Merge pull request #2409 from kosh-b/kafka-heartbeat-passed-to-endpoint
docs (microservices): add documentation for KafkaContext.heartbeat usage
2 parents 6cdec8a + aaabfc3 commit 818c543

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

content/microservices/kafka.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,25 @@ interface IncomingMessage {
342342
}
343343
```
344344

345+
If your handler involves a slow processing time for each received message you should consider using the `heartbeat` callback. To retrieve the `heartbeat` function, use the `getHeartbeat()` method of the `KafkaContext`, as follows:
346+
347+
```typescript
348+
@@filename()
349+
@MessagePattern('hero.kill.dragon')
350+
async killDragon(@Payload() message: KillDragonMessage, @Ctx() context: KafkaContext) {
351+
const heartbeat = context.getHeartbeat();
352+
353+
// Do some slow processing
354+
await doWorkPart1();
355+
356+
// Send heartbeat to not exceed the sessionTimeout
357+
await heartbeat();
358+
359+
// Do some slow processing again
360+
await doWorkPart2();
361+
}
362+
```
363+
345364
#### Naming conventions
346365

347366
The Kafka microservice components append a description of their respective role onto the `client.clientId` and `consumer.groupId` options to prevent collisions between Nest microservice client and server components. By default the `ClientKafka` components append `-client` and the `ServerKafka` components append `-server` to both of these options. Note how the provided values below are transformed in that way (as shown in the comments).

0 commit comments

Comments
 (0)