# generic-service-bus

> > 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.

Latest version **1.0.22** (published 2021-07-19) · ISC license · 0 weekly downloads

## Install

```sh
npm install generic-service-bus
pnpm add generic-service-bus
yarn add generic-service-bus
bun add generic-service-bus
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.22 |
| Published | 2021-07-19 |
| First published | 2021-06-28 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 5 |
| Unpacked size | 274.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Manuel Rodriguez |
| Maintainers | rdgztorres19 |

## Links

- npm: https://www.npmjs.com/package/generic-service-bus
- npm.io page: https://npm.io/package/generic-service-bus

## Dependencies (5)

- [url](https://npm.io/package/url.md) ^0.11.0
- [mqtt](https://npm.io/package/mqtt.md) ^4.2.8
- [amqplib](https://npm.io/package/amqplib.md) ^0.8.0
- [ioredis](https://npm.io/package/ioredis.md) ^4.27.6
- [polly-js](https://npm.io/package/polly-js.md) ^1.8.2

## Recent versions

- 1.0.22 (latest) — 2021-07-19
- 1.0.21 — 2021-07-19
- 1.0.20 — 2021-07-19
- 1.0.18 — 2021-07-15
- 1.0.16 — 2021-07-15
- 1.0.14 — 2021-07-15
- 1.0.12 — 2021-07-14
- 1.0.10 — 2021-07-14
- 1.0.8 — 2021-07-14
- 1.0.6 — 2021-07-12
- 1.0.4 — 2021-07-12
- 1.0.3 — 2021-07-01
- 1.0.2 — 2021-06-30
- 1.0.1 — 2021-06-28
- 1.0.0 — 2021-06-28

## README

## 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.

```bash
npm install generic-service-bus
```
## Create Command Class
```ts
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

```ts
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 
```ts
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

```ts
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

```ts
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);
```

---
_Source: https://npm.io/package/generic-service-bus · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
