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
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:
;
exchanges
define the exchanges you want to usequeues
define the queues availablebinding
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
;await broker.connect; await broker.addExchange'test', 'topic', 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
;await broker.connect; broker.addQueue'testQ', 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
;await broker.connect; broker.addBinding'testX', 'testQ', 'tsemach.#';broker.init;