Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

amqplib

amqp-node4.3mMIT0.10.5TypeScript support: definitely-typed

An AMQP 0-9-1 (e.g., RabbitMQ) library and client.

AMQP, AMQP 0-9-1, RabbitMQ

readme

AMQP 0-9-1 library and client for Node.JS

NPM version NPM downloads Node.js CI amqplib

npm install amqplib

A library for making AMQP 0-9-1 clients for Node.JS, and an AMQP 0-9-1 client for Node.JS v10+.

This library does not implement AMQP 1.0 or AMQP 0-10.

Project status:

  • Expected to work
  • Complete high-level and low-level APIs (i.e., all bits of the protocol)
  • Stable APIs
  • A fair few tests
  • Measured test coverage
  • Ports of the RabbitMQ tutorials as examples
  • Used in production

Still working on:

  • Getting to 100% (or very close to 100%) test coverage

Callback API example

const amqplib = require('amqplib/callback_api');
const queue = 'tasks';

amqplib.connect('amqp://localhost', (err, conn) => {
  if (err) throw err;

  // Listener
  conn.createChannel((err, ch2) => {
    if (err) throw err;

    ch2.assertQueue(queue);

    ch2.consume(queue, (msg) => {
      if (msg !== null) {
        console.log(msg.content.toString());
        ch2.ack(msg);
      } else {
        console.log('Consumer cancelled by server');
      }
    });
  });

  // Sender
  conn.createChannel((err, ch1) => {
    if (err) throw err;

    ch1.assertQueue(queue);

    setInterval(() => {
      ch1.sendToQueue(queue, Buffer.from('something to do'));
    }, 1000);
  });
});

Promise/Async API example

const amqplib = require('amqplib');

(async () => {
  const queue = 'tasks';
  const conn = await amqplib.connect('amqp://localhost');

  const ch1 = await conn.createChannel();
  await ch1.assertQueue(queue);

  // Listener
  ch1.consume(queue, (msg) => {
    if (msg !== null) {
      console.log('Received:', msg.content.toString());
      ch1.ack(msg);
    } else {
      console.log('Consumer cancelled by server');
    }
  });

  // Sender
  const ch2 = await conn.createChannel();

  setInterval(() => {
    ch2.sendToQueue(queue, Buffer.from('something to do'));
  }, 1000);
})();

Running tests

npm test

To run the tests RabbitMQ is required. Either install it with your package manager, or use docker to run a RabbitMQ instance.

docker run -d --name amqp.test -p 5672:5672 rabbitmq

If prefer not to run RabbitMQ locally it is also possible to use a instance of RabbitMQ hosted elsewhere. Use the URL environment variable to configure a different amqp host to connect to. You may also need to do this if docker is not on localhost; e.g., if it's running in docker-machine.

One public host is dev.rabbitmq.com:

URL=amqp://dev.rabbitmq.com npm test

NB You may experience test failures due to timeouts if using the dev.rabbitmq.com instance.

You can run it under different versions of Node.JS using nave:

nave use 10 npm test

or run the tests on all supported versions of Node.JS in one go:

make test-all-nodejs

(which also needs nave installed, of course).

Lastly, setting the environment variable LOG_ERRORS will cause the tests to output error messages encountered, to the console; this is really only useful for checking the kind and formatting of the errors.

LOG_ERRORS=true npm test

Test coverage

make coverage
open file://`pwd`/coverage/lcov-report/index.html

changelog

Change log for amqplib

Changes in v0.10.5

git log v0.10.4..v0.10.5

Changes in v0.10.4

Changes in v0.10.3

git log v0.10.2..v0.10.3

Changes in v0.10.2

git log v0.10.1..v0.10.2
  • Use Buffer.allocUnsafe when sending messages to improve performance (PR 695, thank you @chkimes and @Uzlopak)

Changes in v0.10.1

git log v0.10.0..v0.10.1
  • Allow servername to be specified via socket options as discussed in issue 697

Changes in v0.10.0

git log v0.9.1..v0.10.0
  • Use Native promises (PR 689, thank you @mohd-akram and @kibertoad)

Changes in v0.9.1

git log v0.9.0..v0.9.1
  • Assorted readme changes
  • Use Array.prototype.push.apply instead of concat in Mux (PR 658, thank you @Uzlopak and @kibertoad)
  • Use Map instead of Object for BaseChannel.consumers (PR 660, thank you @Uzlopak)
  • Delete consumer callback after cancellation to free memory (PR 659, thank you @Uzlopak and @kibertoad)

Changes in v0.9.0

git log v0.8.0..v0.9.0
  • Update mocha and replace the deprecated istanbul with nyc (PR 681
  • Update url-parse (PR 675, thank you @suhail-n and @kibertoad)
  • fix: done called twice on invalid options (PR 667, thank you @luddd3 and @kibertoad)
  • Close connection to server on connect errors (PR 647, thank you @luddd3 and @kibertoad)
  • Modernise channel_model.js (PR 635, thank you @kibertoad and @jimmywarting)
  • Bring package-lock.json up to date (PR 653
  • Update url-parse (PR 652, thank you @giorgioatanasov and @buffolander)
  • Modernise channel_model.js (PR 651, thank you for the review @kibertoad)
  • Modernise bitset.js (PR 634, thank you @kibertoad and @jimmywarting)
  • :warning: Drop CI for node versions below 10 (PR 631, thank you for the review @kibertoad)
  • Replace safe-buffer dependency with native buffers (PR 628, thank you @kibertoad and @jimmywarting)

Changes in v0.8.0

git log v0.7.1..v0.8.0
  • :warning: Support for NodeJS prior to v10 is dropped :warning: (PR 615, thank you @xamgore and everyone who helped there)
  • Use hostname as TLS servername, to help with using servers behind load balancers (PR 567, thanks to @carlhoerberg and commenters)

Changes in v0.7.1

git log v0.7.0..v0.7.1
  • Update url-parse (and others) (PR 607, thanks @ThomasGawlitza)

Changes in v0.7.0

git log v0.6.0..v0.7.0
  • Extend support to Node.js v15
  • Fix use of stream.write in tests

Changes in v0.6.0

git log v0.5.6..v0.6.0
  • Extend support to Node.js v14

Changes in v0.5.6

git log v0.5.5..v0.5.6
  • Increase size of encoding space for message headers, to fit e.g., JWT (PR 545); thanks @twatson83
  • Switch to a non-deprecated UUID module (PR 528); thanks to @StrayBird-ATSH
  • Fix a bug in multiplexing that caused an assertion to fail (PR 503); thanks @johanneswuerbach

Changes in v0.5.5

git log v0.5.3..v0.5.5

NB this includes a minor but possibly breaking change: after PR 498, all confirmation promises still unresolved will be rejected when their associated channel is closed.

  • Generate defs in npm prepare rather than npm prepublish so that e.g., amqplib can be installed via git (part of PR 498)
  • Reject all pending confirmations when the channel is closed (PR 498); thanks @johanneswuerbach
  • Update supported NodeJS versions in package.json (PR 525); thanks @tingwai

(Deprecated v0.5.4)

This release was mistakenly published without the generated file ./defs.js. It has been deprecated in favour of v0.5.5.

Changes in v0.5.3

git log v0.5.2..v0.5.3

Changes in v0.5.2

git log v0.5.1..v0.5.2
  • Increase encoding buffer to accommodate large header values (PR 367)
  • Bring code up to date with new Buffer interface (PR 350)
  • Fix dangling connection problem (PR 340)
  • Clear up URL credentials parsing (PR 330)
  • Allow connection params to be suppied in object (PR 304)
  • Support explicit numeric types in field tables (e.g., headers) (PR 389, from a suggestion in issue 358)

Thank you to all contributors, of PRs, issues and comments.

Changes in v0.5.1

git log v0.5.0..v0.5.1
  • Fix mistake in closeBecause (PR 298; thanks to @lholznagel and others who reported the issue, and to @nfantone for the rapid fix)

Changes in v0.5.0

git log v0.4.2..v0.5.0
  • Port to use bluebird rather than when.js (PR 295; thanks to @nfantone, and special mention to @myndzi for #158)
  • Fixed a problem with using channel.get in the callback model (PR 283; good catch, @shanksauce)
  • Added an example that uses generators (thanks @rudijs)
  • Fixed a link in the comments relating to heartbeats (thanks @tapickell)

Changes in v0.4.2

git log v0.4.1..v0.4.2

  • Better documentation and examples
  • Replace uses of ES6 keyword 'await'

Changes in v0.4.1

git log v0.4.0..v0.4.1

  • Tested in Node.JS 0.8 through 4.2 and 5.5
  • Emit an error with the 'close' event if server-initiated

Changes in v0.4.0

git log v0.3.2..v0.4.0

  • Tested on Node.JS 0.8 through 4.0 (and intervening io.js releases)
  • Change meaning of 'b' fields in tables to match RabbitMQ (and AMQP specification)
  • Can now pass an object in place of connection URL (PR 159; thanks to @ben-page)
  • Operator-initiated connection close no longer results in 'error' event (issue 110)
  • Channel and Connection errors have now a .code field with the AMQP reply-code, which may help distinguish error cases (PR 150; thanks to @hippich)
  • Connection.close will resolve to an error if the connection is already closed (issue 181)
  • Connection establishment will resolve with an error if the TCP-level connection or the handshake times out (PR 169; thanks to @zweifisch and @RoCat, who both submitted fixes)
  • Add the maxPriority option as an alias for the 'x-max-priority' queue argument (PR 180; thanks to @ebardes)

Changes in v0.3.2 (since v0.3.1)

git log v0.3.1..v0.3.2

  • Make the engine specification more flexible to admit io.js releases

Changes in v0.3.1 (since v0.3.0)

git log v0.3.0..v0.3.1

Fixes

  • Fail in the right way when a channel cannot be allocated issue 129
  • Make waitForConfirms work properly in callback API PR 116

Enhancements

  • Two new options while connecting: timeout and keep alive (thanks to @rexxars and @jcrugzz respectively)

Changes in v0.3.0 (since v0.2.1)

git log v0.2.1..v0.3.0

Enhancements

  • Allow additional client properties to be set for a connection Issue 98 and PR 80
  • New method in channel API to wait for all unconfirmed messages Issue 89
  • Now supports RabbitMQ's EXTERNAL authentication plugin Issue 105

Changes in v0.2.1 (since v0.2.0)

Fixes

  • Do tuning negotation properly PR 84

Changes in v0.2.0 (since v0.1.3)

git log v0.1.3..v0.2.0

Fixes

  • Correctly deal with missing fields (issue 48)

Enhancements

  • Added a callback-oriented API, parallel to the existing, promise-oriented API.
  • The response to assertExchange now contains the exchange name, analagous to assertQueue (issue 49)
  • The channel method prefetch now has a global flag, to be compatible with newer RabbitMQ.

Changes in v0.1.3 (since v0.1.2)

git log v0.1.2..v0.1.3

Enhancements

  • Add support in the API for using Basic.Reject rather than Basic.Nack, the latter of which is a RabbitMQ extension and not in older versions of RabbitMQ.

Changes in v0.1.2 (since v0.1.1)

git log v0.1.1..v0.1.2

Fixes

  • Restore support for publishing zero-length messages

Enhancements

Changes in v0.1.1 (since v0.1.0)

git log v0.1.0..v0.1.1

Fixes

  • Safer frame construction, no longer relies on allocating a large, fixed-size buffer and hoping it's big enough
  • The ports of RabbitMQ tutorials now avoid a race between publishing and closing the connection

Enhancements

  • Support for RabbitMQ's consumer priority extension
  • Support for RabbitMQ's connnection.blocked extension
  • Better write speed from batching frames for small messages
  • Other minor efficiency gains in method encoding and decoding
  • Channel and connection state errors (e.g., trying to write when closed) include a stack trace from when they moved to that state
  • The arguments table, passed as an option to some methods, can include fields in its prototype chain
  • Provide the more accurately named persistent as a near equivalent of deliveryMode

Changes in v0.1.0 (since v0.0.2)

git log v0.0.2..v0.1.0

Breaking changes

  • Consumer callbacks are invoked with null if the consumer is cancelled (see RabbitMQ's consumer cancel notification)
  • In confirm channels, instead of #publish and #sendToQueue returning promises, they return a boolean as for normal channels, and take a Node.JS-style function (err, ok) callback for the server ack or nack

Fixes

  • Overlapping channel and connection close frames are dealt with gracefully
  • Exceptions thrown in consumer callbacks are raised as 'error' events
  • Zero-size messages are handled
  • Avoid monkey-patching Buffer, and eschew require('util')._extend

Enhancements

  • Channels now behave like Writable streams with regard to #publish and #sendToQueue, returning a boolean from those methods and emitting 'drain'
  • Connections now multiplex frames from channels fairly
  • Low-level channel machinery is now fully callback-based