1.1.4 • Published 3 years ago

msek-rabbitmq-library v1.1.4

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

msek-rabbitmq-library

How to use?

Produce task from a microservice

// Service load the RabbitMQ connection string parameters from config, and build BrokerConnectionString in intialize phase.
// Intialize the MessageBroker for the service
const service = "user-service";
const broker = new MessageBroker(service, "localhost", 5672);

// Enqueue task
const context = { id: 1, name: "Moustafa" };
await broker.enqueueTask("user.new-user-created", context);

Consume task from a microservice

const service = "invoice-service";
const cs = new BrokerConnectionString("localhost", 5672);
const broker = new MessageBroker(cs, serviceName);

// Implement IBrokerConsumer to perform the task
class InvoiceConsumer implements IBrokerConsumer {
  private static _instance: InvoiceConsumer;

  static getInstance(): IBrokerConsumer {
    if (!InvoiceConsumer._instance) {
      this._instance = new InvoiceConsumer();
    }

    return this._instance;
  }

  perform(msg: IBrokerMessage): void {
    // the recieved message
    console.log(msg.context);
  }
}

// In intialize phase register the consumers
await broker.registerConsumer(
  InvoiceConsumer.getInstance(),
  "user.new-user-created"
);
1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.0

3 years ago