@actbox/queue
v2.1.1
Published
It stores the queue lib for ActBox services communication
Downloads
7
Readme
ActBox Queue
This module defines the interface for messaging broker services in order to communicate the rest of ActBox services
Setup
Before to start to use the library, just take in mind, you need to install and run rabbitMQ.
Tip Remenber you could run rabbitMQ effortlessly with Docker Docker Hub
Configuration
These are the available config options for making queues. Only the queueName
field is required.
{
queueName: "my-queue", // take in mind, you need to use the same name in producer and consumer entities as rabbitMQ docs explains
/**
* by default, connectionURI is "amqp://localhost" and the port is the default from rabbitMQ, it is "5672"
**/
connectionURI: "amqp://localhost", // it represents the end-point where rabbitMQ is allocated
forceNACK: false // https://www.rabbitmq.com/confirms.html
}
Development
Once you have cloned queue module:
cd queue && npm run build
On backend and file-service modules, replace amqplib with the @actbox/queue:
"dependencies": {
...
"amqplib": "^0.6.0", (remove this dependency)
"@actbox/queue": "file:{relativePathToQueueModule}",
...
}
Usage
Work Queues
It depends on you start a consumer or producer, you have to start once any of them.
For producers:
const { workQueue } = require('@actbox/queue');
const { startProducer, sendToQueue } = workQueue;
await startProducer({ queueName: 'my-queue' });
const message = { name: 'actBox rules! 😊' };
sendToQueue(message);
For consumers:
const { workQueue } = require('@actbox/queue');
const { startConsumer, consumeFromQueue } = workQueue;
await startConsumer({ queueName: 'my-queue' });
consumeFromQueue().listen(async (message) => {
message.content; // { name: 'actBox rules! 😊' }
});
RPC
For servers:
const { rpc } = require('@actbox/queue');
const { startServer } = rpc;
const myQueueServer = await startServer({ queueName: 'my-queue' });
myQueueServer.listen(async (message) => {
try {
const response = await anyAsyncTask();
return { response }
} catch(err) {
return {
error: {
message: err.message
}
}
}
});
For clients:
const { rpc } = require('@actbox/queue');
const { startClient } = rpc;
const myQueueClient = await startClient({ queueName: 'my-queue' });
myQueueClient.send({ data: 'some data to server' });
myQueueClient.listen((message, error) => {
if(error) {
// handle error as error.message
}
// handle response message
});