RabbitMQ wrapper for PubSub, RPC, Queue based on official examples for amqplib
Init
const RabbitMQ = require('env-rmq').RabbitMQ;
const rmq = new RabbitMQ({
uri: 'amqp://user:password@localhost/'
});
rmq.connect().then(() => {
console.log('Ready.');
}).catch(err => {
console.log('Error: ', err);
});
Queue
Worker - handle task
rmq.worker('my-queue-name', function (msg, reject, ack) {
ack();
});
Client - send task
rmq.worker('my-queue-name');
rmq.worker('my-queue-name').send({
someField: "Hello world!"
});
PubSub
Listener
rmq.pubsub('my-pub-name', function (msg, reject, ack) {
ack();
});
Sender
rmq.pubsub('my-pub-name');
rmq.pubsub('my-pub-name').send({
someField: "Hello world!"
});
RPC
Server
rmq.rpc('my-function', function (msg, reject, ack) {
ack({
returnValue:"OK!"
});
});
Client
rmq.rpc('my-function');
rmq.rpc('my-function').call({
someField: "Hello world!"
}).then(result => {
console.log("RETURNED VALUE:", result);
});