abtool-amqp

1.0.16 • Public • Published

INSTALLATION

npm install --save oacii-amqp

Setting up a connection

const AMQP = require('abtool-amqp');
const amqp = new AMQP('amqp://localhost:5672');

Usage


request-reply

Process B makes an rpc request to 'add' queue and received by Process A 'add' queue handler. The handler sends a reply by returning an object.

NOTE: message to send and return value must be an object

Process A:

// receive a request
amqp.reply('add', message => {
  return {
    result: message.num1 + message.num2
  };
});

Process B:

// send a request
amqp.request('add', { num1: 10, num2: 15 }).then(rep => {
  console.log(rep.result); // 25
});

send - listen

Make a simple sending and receiving of messages in a 1:1 sender:listener fashion. Process B send a message every 1 second to Process A.

Process A:

// listen to an event
amqp.listen('event', message => {
  console.log(message); // { msg: 'hello from Process B'}
});

Process B:

setInterval(() => {
  amqp.send('event', { msg: 'hello from Process 1' });
}, 1000);

publish - subscribe

Make a simple sending and receiving of messages in a 1:N fashion. Looks similar to send - listen, the difference is one sends a message and any subsribers can receive.

Process A:

amqp.subscribe('broadcast', message => {
  console.log(message); // { msg: 'this is a broadcast message'}
});

Process B:

amqp.subscribe('broadcast', message => {
  console.log(message); // { msg: 'this is a broadcast message'}
});

Process C:

setInterval(() => {
  amqp.publish('broadcast', { msg: 'this is a broadcast message' });
}, 1000);

Readme

Keywords

none

Package Sidebar

Install

npm i abtool-amqp

Weekly Downloads

0

Version

1.0.16

License

ISC

Unpacked Size

9.14 kB

Total Files

4

Last publish

Collaborators

  • oacii