npm install --save oacii-amqp
const AMQP = require('abtool-amqp');
const amqp = new AMQP('amqp://localhost:5672');
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
});
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);
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);