A wrapper over node-nats, designed to easily integrate with your code. Taşuu (ташуу) is 'transport' in Kyrgyz.
npm i tasu
Create an instance of the wrapper:
const Tasuu = require('tasu');
const tasu = new Tasuu({group: 'some-service'});
tasu.on('connect', () => {
// wrapper is ready at this point
});
tasu.on('error', (error) => {
throw error;
});
Publish a request and get a response via async/await:
async () => {
const {bar} = await tasu.request('foo', {arg: 1});
...
};
Note: this method uses requestOne
inside, no need to worry about max
responses
Subscribe and respond to a request:
tasu.listen('foo', ({arg}, respond) => {
...
respond(error, {bar: 2});
});
Note: a listener is automatically added to queue group some.request.listeners
Publish an event:
tasu.publish('some.package.sent', {...});
Subscribe and process as worker queue:
tasu.process('*.package.sent', (pack, subject) => {
console.log(subject, pack);
});
listen
, subscribe
and process
methods return an integer subscription ID (SID) which can be used to unsubscribe from a subject:
const sid = tasu.process('*.package.sent', (pack, subject) => {
console.log(subject, pack);
});
// ...
tasu.unsubscribe(sid);
Close NATS connection (if needed):
tasu.close();
NATS_LOG_LEVEL
- set wrapper's log level. Default isdebug