1.0.22 • Published 3 years ago

generic-service-bus v1.0.22

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

Installation

Note: This installation guide is for usage with TypeScript, if you wish to use Generic Service Bus without Typescript please read the documentation about how get started.

npm install generic-service-bus

Create Command Class

import { ICommand } from "generic-service-bus";

export class CreateTenantCommand extends ICommand {
  id: string;
  name: string;
  email: string;
  constructor(id, name, email) {
    super();
    this.id = id;
    this.name = name;
    this.email = email;
  }
}

Create Command Handler Class

import { ICommandHandler, ICorrelationContext } from "generic-service-bus";
import { CreateTenantCommand } from "../../messages/commands/create-tenant-command";

export class CreateTenantCommandHandler
  implements ICommandHandler<CreateTenantCommand> {
  constructor() {}

  handle(command: CreateTenantCommand, ctx: ICorrelationContext): Promise<void> {
    console.log(command, ctx);
    return;
  }
}

Create Event Class

import { IEvent } from "generic-service-bus";

export class UserCreatedEvent extends IEvent {
  id: string;
  name: string;
  constructor(id: string, name: string) {
    super();
    this.id = id;
    this.name = name;
  }
}

Create Event Handler Class

import { IEventHandler, ICorrelationContext } from "generic-service-bus";
import { UserCreatedEvent } from "../../messages/events/user-created-event";

export class UserCreatedEventHandler
  implements IEventHandler<UserCreatedEvent> {
  constructor() {}

  handle(event: UserCreatedEvent, context: ICorrelationContext): Promise<void> {
    console.log(event, context);
    return;
  }
}

Basic Usage

import { ServiceBusBuilder } from "generic-service-bus";

/*const builder = ServiceBusBuilder.useAMQP()
      .withNamespace("tenant-api")
      .withHost("localhost")
      .withPort(5672)
      .withUsername("guest")
      .withPassword("guest")
      .withLocale();*/

    const builder = ServiceBusBuilder.useMQTT()
      .withNamespace("tenant-api")
      .withBroker("localhost")
      .withPort(1883)
      .withUsername("guest")
      .withPassword("guest");

    const busClient = await builder.build();

    busClient.busSubscriber()
    .SubscribeEvent<UserCreatedEvent>("tenant-api", new UserCreatedEventHandler(), null)
    .SubscribeCommand<CreateTenantCommand>("tenant-api", new CreateTenantCommandHandler(), null);
    
    setInterval(() => {

      busClient.busPublisher().SendAsync(
        new CreateTenantCommand("id", "sport.com", "support@sport.com"),
        {
          resource: "tenant",
          name: "create-tenant",
          createdAt: new Date().getTime()
        }
      );

      const messageStatus = busClient.busPublisher().PublishAsync(
        new UserCreatedEvent("1", "John"),
        {
          resource: "tenant",
          name: "user-created",
          createdAt: new Date().getTime()
        }
      );

      console.log("-------------------------------------------------------------------",);
      console.log("Message received: ", messageStatus == MessageStatus.SUCCESS);
      console.log("-------------------------------------------------------------------",);
      
    }, 3000);
1.0.22

3 years ago

1.0.21

3 years ago

1.0.20

3 years ago

1.0.18

3 years ago

1.0.16

3 years ago

1.0.14

3 years ago

1.0.10

3 years ago

1.0.8

3 years ago

1.0.12

3 years ago

1.0.6

3 years ago

1.0.4

3 years ago

1.0.2

3 years ago

1.0.3

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago