feathers-rabbit-queue
v1.5.0
Published
A Feathers service addapter for RabbitMQ based on rabbit-queue
Downloads
15
Readme
feathers-rabbit-queue
A Feathers amqp service adapter for Rabbit Queue.
$ npm install --save feathers-rabbit-queue
API
configure(options)
Configure RabbitMQ with Feathers application as new provider.
Options:
uri
(required) - Connection uri to RabbitMQprefetch
(optional, default:1
) - Prefetch from queueprefix
(optional) - Prefix all queues with an application namesocketOptions
(optional, default:{}
) - Socket Options will be passed as a second param to amqp.connect and from there to the socket library (net or tls)queue
(optional, default:{retries: 3, retryDelay: 1000, logEnabled: true}
) - Queue optionsreconnectDelay
(optional, default:1000
) - Delay before next reconnect attemptlogger
(optional, default:console
) - Output logger
Example
Here's a example of configuration.
const compress = require('compression');
const helmet = require('helmet');
const cors = require('cors');
const feathers = require('@feathersjs/feathers');
const configuration = require('@feathersjs/configuration');
const express = require('@feathersjs/express');
const rabbitConfigure = require('feathers-rabbit-queue').configure;
const app = express(feathers());
// Load app configuration
app.configure(configuration());
// Enable security, CORS, compression, favicon and body parsing
app.use(helmet());
app.use(cors());
app.use(compress());
app.use(express.json());
app.use(express.urlencoded({extended: true}));
// Set up Plugins and providers
app.configure(express.rest());
app.configure(
rabbitConfigure({
uri: 'amqp://user:pass@host:5672',
queueName: 'custom-queue-name',
logger: console
})
);
// Configure a middleware for 404s and the error handler
app.use(express.notFound());
app.use(express.errorHandler({logger}));
Send custom message through RabbitMQ.
Create
async function customFunctionWithAppReference(app) {
try {
const result = await app.rabbit.createMessage({path: 'path/to/somewhere', method: 'create', body: {}});
console.log('Got result:', result);
} catch (error) {
console.log('Got error:', error);
}
}
Patch
async function customFunctionWithAppReference(app) {
try {
const result = await app.rabbit.createMessage({
path: 'path/to/somewhere/id-to-patch',
method: 'patch',
body: 'id-to-patch',
data: {
attribute: 'value'
}
});
console.log('Got result:', result);
} catch (error) {
console.log('Got error:', error);
}
}
Remove
async function customFunctionWithAppReference(app) {
try {
const result = await app.rabbit.createMessage({
path: 'path/to/somewhere/id-to-remove',
method: 'remove',
body: 'id-to-remove',
query: {
attribute: 'value'
}
});
console.log('Got result:', result);
} catch (error) {
console.log('Got error:', error);
}
}
Message can be send to another Queue.
async function customFunctionWithAppReference(app) {
try {
const result = await app.rabbit.createMessage({queueName: 'another-custom-queue', path: 'example/id-of-this-example', method: 'update', body: {}});
console.log('Got result:', result);
} catch (error) {
console.log('Got error:', error);
}
}