0.10.0 • Published 4 years ago
onewallet.library.rabbit v0.10.0
Rabbit
A microservice framework powered by RabbitMQ.
RPC
import Rabbit from 'rabbit';
const rabbit = new Rabbit();
async main() {
await rabbit.createWorker(
'myscope',
async message => message
);
const sendRequest = await rabbit.createClient('myscope');
const result = await sendRequest('Hello World!');
assert.equal(result, 'Hello World!');
}
main();
Fire and Forget
import Rabbit from 'rabbit';
const rabbit = new Rabbit();
async main() {
await rabbit.createWorker(
'myscope',
async message => assert.equal(result, 'Hello World!')
);
const sendMessage = await rabbit.createClient('myscope', { noReponse: true });
sendMessage('Hello World!');
}
main();
PubSub
import Rabbit from 'rabbit';
const rabbit = new Rabbit();
async main() {
await rabbit.createSubscriber(
'myscope',
async message => assert.equal(message, 'Hello World!'),
{ topics: ['mytopic'] }
);
const publish = await rabbit.createPublisher('myscope');
await publish('mytopic', 'Hello World!');
}
main();
API
new Rabbit([options])
const rabbit = new Rabbit();
const rabbit = new Rabbit({
uri: 'amqp://localhost',
prefix: '',
});
Arguments
options
- Rabbit options.options.uri
- RabbitMQ URI. Default value is'amqp://localhost'
.options.prefix
- Queue/Exchange name prefix. Default value is''
.
rabbit.createClient(scope[, options])
const sendMessage = await rabbit.createClient('myscope');
const sendMessage = await rabbit.createClient('myscope', {
timeout: 60000,
noResponse: false,
});
Arguments
scope
- Scope.required
.options
- Client options.options.timeout
- Time until request is dropped. Default value is60000
.options.noResponse
- If set totrue
, client will not wait for response. Default value isfalse
.
rabbit.createWorker(scope, handler[, options])
await rabbit.createWorker('myscope', async message => {
// handle message
});
await rabbit.createWorker(
'myscope',
async message => {
// handle message
},
{
concurrency: 1,
}
);
Arguments
scope
- Scope.required
.handler
- Function to be executed when worker receives a message.required
.options
- Worker options.options.concurrency
- Number of messages that the worker can handle at the same time . Default value is1
.
rabbit.createPublisher(scope)
const publish = await rabbit.createPublisher('scope');
Arguments
scope
- Scope.required
.
rabbit.createSubscriber(scope, handler[, options])
await rabbit.createSubscriber('myscope', async message => {
// handle message
});
await rabbit.createSubscriber(
'myscope',
async message => {
// handle message
},
{ topics: ['*'], concurrency: 1 }
);
Arguments
scope
- Scope.required
.handler
- Function to be executed when subscriber receives a message.required
.options
- Subscriber options.options.topics
- Used to filter messages. Default value is[]
.options.concurrency
- Number of messages that the subscriber can handle at the same time. Default value is1
.
rabbit.stop()
Gracefully shut down all channels.
await rabbit.stop();
0.10.0
4 years ago