Loopback Component for working with a Message Queue
Last updated 3 years ago by mrfelton .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install loopback-component-mq 
SYNC missed versions from official npm registry.


Greenkeeper badge Loopback Component for working with a Rabbit Message Queue.

Circle CI Dependencies Coverage Status


This component provides a convenient way to work with RabbitMQ within a loopback application. This includes:

  • Defining a RabbitMQ topology using the component-config.json
  • Registering message producers and consumers handlers using a mixin.
  • Inspecting RabbitMQ stats and queue statuses using a RabbitMQ loopback model.

Most of the functionality is enabled through the component configuration where you can define your RabbitMQ topology and configure access to the RabbitmQ stats.

In addition, an optional mixin is provided that provides an easy way to attach message producer and consumer helper methods directly to your loopback models.


npm install --save loopback-component-mq

Component Config

Create a component-config.json file in your server folder (if you don't already have one) and configure options inside component-config.json. (see "Component Configuration"" section)

"../node_modules/loopback-component-mq/lib": {
  "options": {
    "restPort": 15672,
    "acls": [{
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$unauthenticated",
      "permission": "DENY"
  "topology": {
    "connection": {
      "uri": "amqp://guest:guest@",
    "exchanges": [{
      "name": "loopback-component-mq:item.write",
      "type": "topic",
      "persistent": true
    "queues": [{
      "name": "loopback-component-mq:client.item.write",
      "subscribe": true
    "bindings": [{
      "exchange": "loopback-component-mq:item.write",
      "target": "loopback-component-mq:client.item.write",
      "keys": ["#"]

The 2 top-level keys are options and topology which are both objects.


The options object has 2 keys:

  • acls (Array, optional), Define ACLs used to protect the RabbitMQ model that gets created.

  • restPort (Number, optional, default: 15672), Define the rest port for your Rabbit management interface.


In the topology object you configure the Rabbit connection, queues, exchanges, and bindings used by this component. Under the hood we use the Rabbot package to establish a connection to and configure the topology for Rabbit.

See https://github.com/arobson/rabbot#configuration-via-json for details on how to configure your topology.

Mixin Config

Add the mixins property to your server/model-config.json:

  "_meta": {
    "sources": [
    "mixins": [

To use with your Models add the mixins attribute to the definition object of your model config.

  "name": "Widget",
  "properties": {
    "name": {
      "type": "string",
  "mixins": {
    "MessageQueue": {
      "producers": {
        "publishItem": {
          "exchange": "item.write",
          "options": {
            "routingKey": "hi",
            "type": "company.project.messages.textMessage",
            "correlationId": "one",
            "contentType": "application/json",
            "messageId": "100",
            "expiresAfter": "1000 // TTL in ms, in this example 1 second",
            "timestamp": "// posix timestamp (long)",
            "mandatory": "true, //Must be set to true for onReturned to receive unqueued message",
            "headers": {
              "random": "application specific value"
            "timeout": "// ms to wait before cancelling the publish and rejecting the promise"
      "consumers": {
        "consumeItem": {
          "queue": "item.write",
          "type": "created"

The MessageQueue object has 2 keys:

  • producers (Object, optional). Use this object to define message queue producer helper methods.

  • consumers (Object, optional). Use this object to define message queue consumer helper methods.


You may use the mixin to define producer methods. These are essentially a wrapper around Rabbot's publish method and provide a convenient way to publish messages to an exchange.

Producers accept 2 parameters:

  • exchange (String, required) Name of the exchange to publish message to.
  • options (Object, optional) Default options passed to the Rabbot publish method.

Defining a producer will result in a static method being added to your Model using the key name as the method name. For example, the above configuration would create the static method Widget.publishItem. When called, a producer method will publish a message containing payload on the specified exchange, using the defined publishing options.

The method created accepts 2 parameters:

  • payload (Any, required) The raw message that will be sent to the defined exchange.
  • options (String|Object, optional) If a string is provided, this will be used as the routing key for the message. You can also provide an options object, which allows you to override any of the default options passed to the Rabbot publish method.


You may use the mixin to define consumer methods which provide a convenient way to define a Rabbot message handler using Rabbot's handle method.

When defining a consumer on a queue, the component will register handler to consumer messages from the specified queue.

For example, the above configuration would register the static method Widget.consumeItem as a message handle for the item.write queue for messages that are using the created routing key.

Consumers accept 2 parameters:

  • queue (String, required) Name of the queue to consumer messages from.
  • type (String, optional) Handle messages with this type name or pattern.

Having defined a consumer in your mixin, you are expected to define the actual consumer method on your model using the key name as the method name. For example, the above configuration would result in the static method Widget.consumeItem being registered as a message handler for messages with a type of created.

NOTE: you are responsible for creating the Widget.consumeItem method - which must accept 1 parameter:

  • payload (Any, required) The raw message body pulled from the message queue.

To acknowledge (ack) the message your method should return a resolved Promise.

To reject (nack) the message your method should return a rejected Promise.


Source files are located in the [lib] directory. Edit the source files to make changes while running npm run dev in the background.

  npm run dev

Run with debugging output on:

  DEBUG=loopback:component:mq* npm run dev


For error checking and to help maintain style this package uses eslint as a pretest.

Run the tests in the test directory.

  npm test

Run with debugging output on:

  DEBUG=loopback:component:mq* npm test



Current Tags

  • 0.0.0-development                                ...           experimental (4 years ago)
  • 2.2.5                                ...           latest (3 years ago)
  • 2.0.0-beta.3                                ...           next (4 years ago)

27 Versions

  • 2.2.5                                ...           3 years ago
  • 2.2.4                                ...           3 years ago
  • 2.2.3                                ...           3 years ago
  • 2.2.2                                ...           3 years ago
  • 2.2.1                                ...           4 years ago
  • 2.2.0                                ...           4 years ago
  • 2.1.1                                ...           4 years ago
  • 2.1.0                                ...           4 years ago
  • 0.0.0-development                                ...           4 years ago
  • 2.0.0                                ...           4 years ago
  • 2.0.0-beta.3                                ...           4 years ago
  • 2.0.0-beta.2                                ...           4 years ago
  • 2.0.0-beta.1                                ...           4 years ago
  • 1.3.4                                ...           4 years ago
  • 1.3.3                                ...           4 years ago
  • 1.3.2                                ...           4 years ago
  • 1.3.1                                ...           4 years ago
  • 1.3.0                                ...           4 years ago
  • 1.2.4                                ...           4 years ago
  • 1.2.3                                ...           4 years ago
  • 1.2.2                                ...           4 years ago
  • 1.2.1                                ...           4 years ago
  • 1.2.0                                ...           4 years ago
  • 1.1.2                                ...           4 years ago
  • 1.1.1                                ...           4 years ago
  • 1.1.0                                ...           4 years ago
  • 1.0.0                                ...           4 years ago
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (5)
  • amqp-stats https://github.com/fullcube/node-amqp-stats/tarball/fe86722fd278067969f03addde57ed53f0910cd8
  • bluebird 3.5.0
  • debug 3.0.0
  • lodash 4.17.4
  • rabbot https://github.com/fullcube/rabbot/tarball/bbd6343777f8f341d592711e8e2bf23fe2ad76f5

Copyright 2014 - 2016 © taobao.org |