Coder Social home page Coder Social logo

semabigcorp / amqplib Goto Github PK

View Code? Open in Web Editor NEW

This project forked from amqp-node/amqplib

0.0 0.0 0.0 1.01 MB

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

Home Page: https://amqp-node.github.io/amqplib/

License: Other

JavaScript 99.38% Makefile 0.62%

amqplib's Introduction

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('Recieved:', 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

amqplib's People

Contributors

ben-page avatar carlhoerberg avatar cressie176 avatar furstenheim-geoblink avatar hippich avatar jcrugzz avatar jfromaniello avatar jimmywarting avatar johanneswuerbach avatar kibertoad avatar luddd3 avatar mohd-akram avatar nfantone avatar olegabr avatar oronnadiv avatar paulrbr-fl avatar posgarou avatar ravshansbox avatar renarsvilnis avatar revington avatar rexxars avatar rikaardhosein avatar rudijs avatar squaremo avatar thomasgawlitza avatar timwatsonruffer avatar uzlopak avatar woozyking avatar xamgore avatar zweifisch avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.