@coolgk/amqp

a simple RabbitMQ (amqp wrapper) class for publishing and consuming messages

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
@coolgk/amqp
2372.0.56 years ago6 years agoMinified + gzip package size for @coolgk/amqp in KB

Readme

@coolgk/amqp

a javascript / typescript module
npm install @coolgk/amqp
a simple RabbitMQ (amqp wrapper) class for publishing and consuming messages
Report bugs here: https://github.com/coolgk/node-utils/issues

Examples

import { Amqp } from '@coolgk/amqp';
// OR
// const { Amqp } = require('@coolgk/amqp');

const amqp = new Amqp({
    url: 'amqp://localhost/vhost'
});

const message = {
    a: 1,
    b: 'b'
};

// CONSUMER MUST BE STARTED FIRST BEFORE PUSHLISHING ANY MESSAGE

// consumer.js
// consume message and return (send) a response back to publisher
amqp.consume(({rawMessage, message}) => {
    console.log('consumer received', message); // consumer received ignore response
                                               // consumer received { a: 1, b: 'b' }
    return {
        response: 'response message'
    }
});

// publisher.js
// publish a message, no response from consumer
amqp.publish('ignore response');

// publish a message and handle response from consumer
amqp.publish(message, ({rawResponseMessage, responseMessage}) => {
    console.log('response from consumer', responseMessage); // response from consumer { response: 'response message' }
});


// example to add:
// consume from (multiple) routes
// round robin consumers
// direct route + a catch all consumer

Amqp

Kind: global class
* [new Amqp(options)](#new_Amqp_new)
* [.closeConnection()](#Amqp+closeConnection) ⇒ <code>void</code>
* [.publish(message, [callback], [options])](#Amqp+publish) ⇒ <code>promise.<Array.<boolean>></code>
* [.consume(callback, [options])](#Amqp+consume) ⇒ <code>promise</code>
* [.getChannel()](#Amqp+getChannel) ⇒ <code>promise</code>

new Amqp(options)

| Param | Type | Description | | --- | --- | --- | | options | object | | | options.url | string | connection string e.g. amqp://localhost | | options.sslPem | string | pem file path | | options.sslCa | string | sslCa file path | | options.sslPass | string | password |

amqp.closeConnection() ⇒ void

Kind: instance method of Amqp

amqp.publish(message, callback, options) ⇒ promise.<Array.<boolean>>

Kind: instance method of Amqp
| Param | Type | Default | Description | | --- | --- | --- | --- | | message | \* | | message any type that can be JSON.stringify'ed | | callback | function | | callback(message) for processing response from consumers | | options | object | | | | options.routes | string \| Array.<string> | "'#'" | route names | | options.exchangeName | string | "'defaultExchange'" | exchange name |

amqp.consume(callback, options) ⇒ promise

Kind: instance method of Amqp
| Param | Type | Default | Description | | --- | --- | --- | --- | | callback | function | | consumer(message) function should returns a promise | | options | object | | | | options.routes | string \| Array.<string> | "'#'" | exchange routes | | options.queueName | string | "''" | queue name for processing messages. consumers with the same queue name process messages in round robin style | | options.exchangeName | string | "'defaultExchange'" | exchange name | | options.exchangeType | string | "'topic'" | exchange type | | options.priority | number | 0 | priority, larger numbers indicate higher priority | | options.prefetch | number | 1 | 1 or 0, if to process request one at a time |

amqp.getChannel() ⇒ promise

Kind: instance method of Amqp
Returns: promise - - promise