Skip to content

dessli/egg-rmq

Repository files navigation

egg-rmq

NPM version build status Test coverage David deps Known Vulnerabilities npm download

egg plugin for RabbitMQ

Install

$ npm i egg-rmq --save

Usage

// {app_root}/config/plugin.js
exports.rmq = {
  enable: true,
  package: 'egg-rmq',
};

Configuration

Single Server
// {app_root}/config/config.default.js
exports.rmq = {
    host: 'amqp://localhost',
};
Multiple Server
// {app_root}/config/config.default.js
exports.rmq = {
    servers: {
      test: {
        host: 'amqp://localhost',
      },
    },
  };
// {app_root}/config/config.default.js
// If not set name then this config like Single Server, use app.rmq.conn
exports.rmq = {
    servers: [
      {
        host: 'amqp://localhost',
      },
      {
        name: 'test',
        host: 'amqp://localhost2',
      },
    ],
  };
Route queue

use rmq.route to set Single Server channel

use rmq[server name].route to set Multiple Server channel

// {app_root}/router.js
module.exports = app => {
  const { router, controller, rmq } = app;
  rmq.route('hello', rmq.controller.listener.test); // hello is queue
};
Controller

use ctx.req.ch to get Channel

use ctx.req.queue to get Queue

use app.rmq.conn to get Single Server connect

use app.rmq[server name].conn to get Multiple Server connect

// {app_root}/rmq/controller/hello.js
module.exports = app => {
  return class TestController extends app.Controller {
    async test() {
      const { ctx } = this;
      await ctx.req.ch.assertQueue(ctx.req.queue, { durable: false });
      ctx.req.ch.consume(ctx.req.queue, msg => {
        console.log(' [x] Received %s', msg.content.toString());
        console.log(msg.properties.headers, '#hello headers');
      }, { noAck: true });
      ctx.req.ch.sendToQueue(ctx.req.queue, Buffer.from('Nodejs'), { headers: { user: 'user test' } });
    }
  };
};

see config/config.default.js for more detail.

Questions & Suggestions

Please open an issue here.

License

MIT

About

egg plugin for RabbitMQ

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published