DEV Community

mark vachi
mark vachi

Posted on • Edited on

Integrating NestJS with Camunda-Zeebe SaaS: A Step-by-Step Guide

Introduction

In today's dynamic software development landscape, orchestrating workflows efficiently is crucial. Camunda-Zeebe, a cloud-native workflow automation platform, seamlessly integrates with NestJS, a powerful Node.js framework. In this tutorial, we'll explore how to set up and connect NestJS with Camunda-Zeebe SaaS, allowing you to manage workflows with ease.

Prerequisites

Before diving into the integration, make sure you have the following prerequisites in place:

  • Node.js and npm installed
  • NestJS application set up
  • Camunda-Zeebe SaaS account

Setting Up Zeebe Microservice

In your main.ts file, register the Zeebe microservice. This establishes the connection between your NestJS application and the Zeebe server.

// main.ts

import { AppModule } from './app.module';
import { NestFactory } from '@nestjs/core';
import { ZeebeServer } from 'nestjs-zeebe';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  const microservice = app.connectMicroservice({
    strategy: app.get(ZeebeServer),
  });

  await app.startAllMicroservices();
  await app.listen(3002);
}
bootstrap();
Enter fullscreen mode Exit fullscreen mode

Configuring ZeebeModule in AppModule

In your app.module.ts file, register the ZeebeModule and configure the connection options. Ensure you replace placeholders with your actual credentials and gateway address.

// app.module.ts

import { ZeebeModule, ZeebeServer } from 'nestjs-zeebe';
import { Module } from '@nestjs/common';

@Module({
  imports: [
    ZeebeModule.forRoot({
      gatewayAddress: '<yourGatewayAddress>',
      options: {
        oAuth: {
          url: 'https://login.cloud.camunda.io/oauth/token',
          audience: 'zeebe.camunda.io',
          clientId: '<yourClientId>',
          clientSecret: '<yourClientSecret>',
        },
      },
    }),
  ],
  // ... other module configurations
})
export class AppModule {}
Enter fullscreen mode Exit fullscreen mode

Remember to replace <yourGatewayAddress>, <yourClientId>, and <yourClientSecret> with your actual Camunda-Zeebe SaaS credentials.

Starting Workflow Instances

In your app.controller.ts file, use the Zeebe client to start a workflow instance.

// app.controller.ts

import { ZEEBE_CONNECTION_PROVIDER } from 'nestjs-zeebe';

@Controller()
export class AppController {
  constructor(@Inject(ZEEBE_CONNECTION_PROVIDER) private readonly zbClient: ZBClient) {}

  @Get()
  async startWorkflow(): Promise<CreateProcessInstanceResponse> {
    return this.zbClient.createProcessInstance({
      bpmnProcessId: 'Process_16ssnwv',  // Replace with your actual BPMN process ID
      variables: {
        // Workflow variables
      },
    });
  }
  // ... other controller methods
}
Enter fullscreen mode Exit fullscreen mode

Replace Process_16ssnwv with your actual BPMN process ID.

Subscribing to Zeebe Events

In your app.controller.ts file, implement a Zeebe worker to subscribe to specific events in the workflow.

// app.controller.ts

@Controller()
export class AppController {
  // ... other controller methods

  @ZeebeWorker('mail')
  mail(@Payload() job: ZeebeJob, @Ctx() context: { worker: ZBWorker<IInputVariables, ICustomHeaders, IOutputVariables> }) {
    console.log('Mail, Task variables', job.variables);

    // Task worker business logic goes here
    console.log('Send mail, success');

    job.complete(updatedVariables);
  }
}
Enter fullscreen mode Exit fullscreen mode

Adjust the worker method (mail in this case) to match your workflow's task name and implement the necessary business logic.

Conclusion

Congratulations! You've successfully integrated NestJS with Camunda-Zeebe SaaS, allowing you to manage workflows effortlessly. This powerful combination opens up a world of possibilities for orchestrating complex business processes in a scalable and efficient manner.

Feel free to explore further and adapt these steps to your specific use case. Happy coding!

Top comments (2)

Collapse
 
m4r14 profile image
mark vachi

NestJS client for Zeebe
A great addition to Zeebe is the NestJs client and the fact that it’s still being so well maintained and widely used.

GitHub logo camunda-community-hub / nestjs-zeebe

Zeebe transport and client for nestjs framework

Sponsored by Precise Finance
Sponsored by Precise

Community extension badge Lifecycle: Stable badge Compatible with: Camunda Platform 8

Nest Logo

NestJS Zeebe Connector (Transport and Client) - Up-to-date

A zeebe transport and client for NestJS 7.x

Using the zeebe-node module and exposing it as a NestJS transport and module.

Build Status

Use version 2.x.x and above for Zeebe 1.x.x and above

Install

npm install nestjs-zeebe

Basic usage

    // app.module.ts
    import { Module } from '@nestjs/common';
    import { AppController } from './app.controller';
    import { ZeebeModule, ZeebeServer } from 'nestjs-zeebe';

    @Module({
    imports: [ ZeebeModule.forRoot({ gatewayAddress: 'localhost:26500' })],
    controllers: [AppController],
    providers: [ZeebeServer],
    })
    export class AppModule {}
Enter fullscreen mode Exit fullscreen mode
    // main.ts
    import { NestFactory } from '@nestjs/core';
    import { AppModule } from './app.module';
    import { ZeebeServer } from 'nestjs-zeebe';
    async function bootstrap() {
    const app = await NestFactory.create(AppModule
…
Enter fullscreen mode Exit fullscreen mode
Collapse
 
danshapir profile image
Dan Shapir

Thanks for the article, as the creator of this package I'm always happy to see people using it and enjoying it!