# props-lib-mq

> Handling lib-mq operations

Latest version **0.0.1** (published 2019-09-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install props-lib-mq
pnpm add props-lib-mq
yarn add props-lib-mq
bun add props-lib-mq
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1 |
| Published | 2019-09-19 |
| First published | 2019-09-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 20 |
| Unpacked size | 42.5 KB |
| Known vulnerabilities | 0 (+2 in 2 direct dependencies) |
| Install scripts | no |
| Author | Props Team |
| Maintainers | propsproject |

## Links

- npm: https://www.npmjs.com/package/props-lib-mq
- npm.io page: https://npm.io/package/props-lib-mq

## Dependencies (20)

- [url](https://npm.io/package/url.md) ^0.11.0
- [chai](https://npm.io/package/chai.md) ^4.1.2
- [uuid](https://npm.io/package/uuid.md) ^3.3.0
- [nconf](https://npm.io/package/nconf.md) ^0.8.4
- [dotenv](https://npm.io/package/dotenv.md) ^5.0.1
- [lodash](https://npm.io/package/lodash.md) ^4.17.15
- [amqplib](https://npm.io/package/amqplib.md) ^0.5.5
- [ioredis](https://npm.io/package/ioredis.md) ^4.14.1
- [ts-node](https://npm.io/package/ts-node.md) ^6.0.5
- [copyfiles](https://npm.io/package/copyfiles.md) ^2.1.1
- [deepmerge](https://npm.io/package/deepmerge.md) ^1.3.1
- [typescript](https://npm.io/package/typescript.md) ^2.8.3
- [@types/chai](https://npm.io/package/@types/chai.md) ^4.1.3
- [@types/node](https://npm.io/package/@types/node.md) ^10.12.0
- [chai-string](https://npm.io/package/chai-string.md) ^1.4.0
- [@types/lodash](https://npm.io/package/@types/lodash.md) ^4.14.138
- [props-lib-env](https://npm.io/package/props-lib-env.md) ^0.0.2
- [chai-as-promised](https://npm.io/package/chai-as-promised.md) ^7.1.1
- [props-lib-logger](https://npm.io/package/props-lib-logger.md) 1.1.1
- [@types/chai-as-promised](https://npm.io/package/@types/chai-as-promised.md) ^7.1.0

## Recent versions

- 0.0.1 (latest) — 2019-09-19

## README

# lib-mq

Connect to RabbitMQ in order to publish / consume messages.

The library is wrapping [amqp.node](https://github.com/squaremo/amqp.node) client library

## Connecting to Rabbit

The following will open a connection with rabbit so later publishing and consuming will be possible.
Since we would like to have one single connection per running node it's best to add this to server.start in app.js
The connect function already wrapping a retry mechanism in case the connection fails. 
```javascript
import { MQClient } from 'props-lib-mq';

MQClient.connect( RABBIT_URL );
```

## Publishing to Rabbit

publish function signature
```javascript
static async publish(exchangeName, exchangeType, key, data, correlationId = null, opts = {}):Promise<boolean>
```

```javascript
import { MQClient } from 'props-lib-mq';

const published =  MQClient.publish('user', 'topic', 'event.success.user.join.room', { user_id: "123", room_id: "123" }).then(published => {
  if(published) console.log('Woot Woot');
  else console.error('Publish Failed');
}).catch(err){
  // Critical error
};
```

## Consuming from rabbit

When consuming we need first to wait for a connection to be available, once that happen a 'ready' event will be emitted.
The event will be emitted on each reconnection made to rabbit in case the connection was suddenly closed and had to reconnect those promising continues consuming subsequent an event of connection close / error.
 
consume function signature
```javascript
static consume(exchangeName, exchangeType, key, queueName, opts = {}, callback)
```

```javascript
import { MQClient, MQClientEvents } from 'props-lib-mq';

MQClientEvents.on('ready', () => {
  MQClient.consume('user', 'topic', 'event.success.user.offline', 'room-event-success-user-offline', {}, (data, ack, nack, rawMsg) => {
    
    // Handle message here
    
  });
});
```

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