0.2.0 • Published 9 months ago

@novice1/amqp-client v0.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

@novice1/amqp-client

AMQP 0-9-1 client. Sends messages with predefined headers. Extends amqplib.

Installation

$ npm install @novice1/amqp-client

Usage

publisher.js

const AMQPClient = require('@novice1/amqp-client');

const publisher = new AMQPClient(
  // url
  'amqp://guest:guest@localhost:5672/',
  // socket options
  null,
  // predefined headers for all messages sent
  {
    clientCode: 'guest101'
  }
);

publisher.connect().then((conn) => {
    return conn.createChannel();
  })
  .then((ch) => {
    return ch
      .assertQueue('queue_name', { durable: false })
      .then(function () {
        return ch.sendToQueue(
            'queue_name',
            Buffer.from('message')
          );
      });
  })
  .catch(console.error);

consumer.js

const AMQPClient = require('@novice1/amqp-client');

const consumer = new AMQPClient(
  // url
  'amqp://guest:guest@localhost:5672/'
);

consumer.connect().then((conn) => {
    return conn.createChannel();
  })
  .then((ch) => {
    return ch
      .assertQueue('queue_name', { durable: false })
      .then(function () {
        return ch.consume('queue_name', (msg) => {
          let senderIP = msg.properties.headers.senderIP;
          let clientCodeHeader = msg.properties.headers.clientCode;
          let body = msg.content.toString();
          console.log(" [x] Received '%s' from '%s' with clientCode '%s'", body, senderIP, clientCodeHeader);
        });
      });
  })
  .catch(console.error);

References

0.2.0

9 months ago

0.1.1

3 years ago

0.1.0

3 years ago