# typescript-rabbitmq

> A wrapper library for using rabbitmq under typescript

Latest version **0.0.8** (published 2018-09-16) · ISC license · 0 weekly downloads

## Install

```sh
npm install typescript-rabbitmq
pnpm add typescript-rabbitmq
yarn add typescript-rabbitmq
bun add typescript-rabbitmq
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.8 |
| Published | 2018-09-16 |
| First published | 2018-08-25 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 10 |
| Unpacked size | 13.3 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Tsemach Mizrachi |
| Maintainers | tsemach |
| Keywords | rabbitmq, typescript, amqplib |

## Links

- npm: https://www.npmjs.com/package/typescript-rabbitmq
- Repository: https://github.com/tsemach/typescript-rabbitmq
- Homepage: https://github.com/tsemach/typescript-rabbitmq#readme
- Issues: https://github.com/tsemach/typescript-rabbitmq/issues
- npm.io page: https://npm.io/package/typescript-rabbitmq

## Dependencies (10)

- [uuid](https://npm.io/package/uuid.md) ^3.3.2
- [assert](https://npm.io/package/assert.md) ^1.4.1
- [amqplib](https://npm.io/package/amqplib.md) ^0.5.2
- [logging](https://npm.io/package/logging.md) ^3.2.0
- [inversify](https://npm.io/package/inversify.md) ^4.13.0
- [@types/uuid](https://npm.io/package/@types/uuid.md) ^3.4.3
- [@types/assert](https://npm.io/package/@types/assert.md) 0.0.31
- [app-root-path](https://npm.io/package/app-root-path.md) ^2.1.0
- [@types/amqplib](https://npm.io/package/@types/amqplib.md) ^0.5.8
- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.12

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.0.8 (latest) — 2018-09-16
- 0.0.7 — 2018-09-11
- 0.0.6 — 2018-09-11
- 0.0.5 — 2018-09-10
- 0.0.4 — 2018-09-05
- 0.0.3 — 2018-08-25
- 0.0.2 — 2018-08-25
- 0.0.1 — 2018-08-25

## README

# typescript-rabbitmq
A broker client library of using rabbitmq in a typescript code

>This version supporting rabbitmq topic configuration.
Next versions will include support for direct and fanout.
 

##Install
````
npm install --save typescript-rabbitmq
````

##API
##**Create**
**`let broker = new Broker(config);`**

See below about the configuration

##**Connect**
**`broker = await this.broker.connect();`**

This will connect to the rabbit server where the host:port defined in the config.

## **Define Queues Callbacks**
Call to **addConsume** method to add queue callback, for example **`broker.addConsume("work.tasks.queue", (msg) => {..});`**

#####Example of using the broker as a member of user class
````typescript
class Receiver {
  broker: Broker;

  constructor() {
    this.broker = new Broker(config);
  }

  async init() {
    await this.broker.connect();

    this.broker.addConsume("work.tasks.queue", this.taskCB.bind(this));
    this.broker.addConsume("work.reply.queue", this.replyCB.bind(this));
  }

  taskCB(msg) {
    ...
  }

  replyCB(msg) {
    ...
  }
}
```` 

This will call **taskCB** and **replyCB** on messages coming to "work.tasks.queue" and "work.reply.queue" accordingly.


## Configuration

Example of configuration looks like that:
````typescript
let config: any = {
    connection: {
        user: process.env.QUEUE_USERNAME,
        pass: process.env.QUEUE_PASSWORD,
        host: process.env.QUEUE_SERVER || 'localhost',
        port: process.env.QUEUE_PORT || '5672',
        timeout: 2000,
        name: "rabbitmq"
    },
    exchanges: [
        {name: "work.tasks.exchange", type: "topic", options: {publishTimeout: 1000, persistent: true, durable: false}},
        {name: "work.reply.exchange", type: "topic", options: {publishTimeout: 1000, persistent: true, durable: false}}
    ],
    queues: [
        {name: "work.tasks.queue", options: {limit: 1000, queueLimit: 1000}},
        {name: "work.reply.queue", options: {limit: 1000, queueLimit: 1000}}
    ],
    binding: [
        {exchange: "work.tasks.exchange", target: "work.tasks.queue", keys: "somekey.#"},
        {exchange: "work.reply.exchange", target: "work.reply.queue", keys: "otherkey.#"}
    ],
    logging: {
        adapters: {
            stdOut: {
                level: 3,
                bailIfDebug: true
            }
        }
    }
};
````

- **`exchanges`** define the exchanges you want to use
- **`queues`** define the queues available 
- **`binding`** the binging of queues with exchanges. 

## Broker Exchange, Queue API  

### **`broker::addExchange`** 
Add a specific exchange.

#### arguments
- *name* : [string] - the name of the exchange.
- *type* : [BrokerExchangeType] - one of 'fanout' | 'direct' | 'topic';
- *options* : [BrokerExchangeOptions] - exchange options, see broker_options.ts.

#### usage

````typescript
let broker = new Broker(config);
await broker.connect();

await broker.addExchange('test', 'topic', {durable: false} as BrokerExchangeOptions);
await broker.init();
````

### **`broker::addQueue`** 
Add a specific queue.

#### arguments
- *name* : [string] - the name of the exchange.
- *type* : [BrokerExchangeType] - one of 'fanout' | 'direct' | 'topic';
- *options* : [BrokerExchangeOptions] - exchange options, see broker_options.ts.

#### usage

````typescript
let broker = new Broker(config);
await broker.connect();

broker.addQueue('testQ', {durable: true} as BrokerQueueOptions);
broker.init();
````

### **`broker::bindQueue`** 
Bind an exchange to queue with a route key.

#### arguments
- *exchange* : [string] - the name of the exchange.
- *queue* : [string] -name of a queue to bind to;
- *route* : [string] - the route key string.

#### usage

````typescript
let broker = new Broker(config);
await broker.connect();
    
broker.addBinding('testX', 'testQ', 'tsemach.#');
broker.init();
````

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