8.0.2 • Published 2 years ago

carrotmq v8.0.2

Weekly downloads
7
License
GPL-3.0
Repository
github
Last release
2 years ago

carrotmq

a much easy way to use rabbitmq

中文文档

Build Status Version npm NPM Downloads Dependencies NPM

APIDOC

documentation

usage

const {CarrotMQ} = require('carrotmq');

const mq = new CarrotMQ('amqp://localhost');
await mq.connect()

const publisher = new CarrotMQ('amqp://localhost'); //also can use without schema
await publisher.connect()

mq.queue('fooQueue', async (data, ctx) => {
    console.log(data);
    ctx.ack();
    //ctx.nack();
    //ctx.reject();
    //ctx.cancel(); cancel this consumer;
    ctx.reply({date: new Date}); //reply to message.properties.relyTo
    ctx.carrotmq //carrotmq instrance
    ctx.channel  //current channel
    return Promise.reject(); // or throw new Error('some thing happened') will execute `this.reject()` if this message hadn't been ack
});

mq.sendToQueue('queue', {msg: 'message'});
mq.publish('exchange', 'foo.bar.key', {msg: 'hello world!'});

RPC

mq.rpc('queue', {data: new Date})
.then((reply)=>{
  reply.ack();
  console.log(reply.data); //some reply result
});

If you prefer to use named queue rather than temp queue, you can set in config like

const mq = new CarrotMQ('amqp://localhost', {
  callbackQueue: {
    queue: 'carrotmq.rpc.callback'
  }
})

Or

mq.rpc('carrotmq.rpc', {data: 'foo'}, 'carrotmq.rpc.callback') 

RPC Over Exchange

app.queue('rpcQueue', async (data, ctx) => {
  await ctx.reply(data);
  await ctx.ack();
});

let time = new Date();
app.rpcExchange('exchange0', 'rpc.rpc', {time})
.then(function (reply){
  reply.ack();
  console.log(reply.data)//{time: time}
}) // if target exchange is an topic or fanout exchange, only the first reply will be accepted.

events

ready

emit after connection established

mq.on('ready', function(){});

error

emit when something happened

mq.on('error', function (err){});

message

emit when message come

mq.on('message', function (data){
  data.channel; //channel object
  data.queue   //queue name
  data.message  //message object
})

close

emit when connection close

mq.on('close', () => setTimeout(mq.connect(), 1000));

upgrade

V4 to V5

Because of rewritten in TypeScript, some export has changed before:

const CarrotMQ = require('carrotmq')

after:

const {CarrotMQ} = require('carrotmq')

V2 to V3

breaking change

  • mq.rpc() and mq.rpcExchange() method remove the 4th consumer argument.And using Promise

    used to

      mq.rpc('someQueue', {data}, function(data) {
        const that = this;
        // or some data async logic
        doSomeThingAsync(data)
        .then(() => that.ack())
        .catch(() => that.nack());
        return data;
      }).then((data) => console.log(data));
now can replaced by
```js
    let reply = await mq.rpc('someQueue', {data});
    try {
      await doSomeThingAsync(reply.data);
      reply.ack();
    } catch (e) {
      reply.nack();
    }
8.0.2

2 years ago

8.0.1

2 years ago

8.0.0

2 years ago

7.0.1

3 years ago

7.0.0

3 years ago

6.2.7

3 years ago

6.2.6

4 years ago

6.2.5

4 years ago

6.2.4

4 years ago

6.2.3

4 years ago

6.2.2

5 years ago

6.2.1

5 years ago

6.2.0

5 years ago

6.1.4

5 years ago

6.1.3

5 years ago

6.1.2

5 years ago

6.1.1

5 years ago

6.1.0

5 years ago

6.0.2

5 years ago

6.0.1

6 years ago

6.0.0

6 years ago

5.7.1

6 years ago

5.7.0

6 years ago

5.6.4

6 years ago

5.6.2

6 years ago

5.6.1

6 years ago

5.6.0

6 years ago

5.5.3

6 years ago

5.5.2

6 years ago

5.5.1

6 years ago

5.5.0

6 years ago

5.4.5

6 years ago

5.4.4

6 years ago

5.4.3

6 years ago

5.4.2

6 years ago

5.4.1

6 years ago

5.3.0

6 years ago

5.2.0

6 years ago

5.1.5

6 years ago

5.1.4

6 years ago

5.1.3

6 years ago

5.1.2

6 years ago

5.1.1

6 years ago

5.1.0

6 years ago

5.0.4

7 years ago

5.0.3

7 years ago

5.0.2

7 years ago

5.0.1

7 years ago

5.0.0

7 years ago

4.2.3

7 years ago

4.2.2

7 years ago

4.2.1

7 years ago

4.2.0

7 years ago

4.1.2

7 years ago

4.1.1

7 years ago

4.1.0

7 years ago

4.0.0

7 years ago

3.0.2

7 years ago

3.0.1

7 years ago

3.0.0

7 years ago

2.7.1

7 years ago

2.7.0

8 years ago

2.6.4

8 years ago

2.6.3

8 years ago

2.6.2

8 years ago

2.6.1

8 years ago

2.6.0

8 years ago

2.5.1

8 years ago

2.5.0

8 years ago

2.4.0

8 years ago

2.3.1

8 years ago

2.3.0

8 years ago

2.2.1

8 years ago

2.2.0

8 years ago

2.1.1

8 years ago

2.1.0

8 years ago

2.0.0

8 years ago

1.4.8

8 years ago

1.4.7

8 years ago

1.4.6

8 years ago

1.4.5

8 years ago

1.4.4

8 years ago

1.4.3

8 years ago

1.4.2

8 years ago

1.4.1

8 years ago

1.4.0

8 years ago

1.3.8

8 years ago

1.3.7

8 years ago

1.3.6

8 years ago

1.3.5

8 years ago

1.3.4

8 years ago

1.3.3

8 years ago

1.3.2

8 years ago

1.3.1

8 years ago

1.3.0

8 years ago

1.2.0

8 years ago

1.1.0

8 years ago

1.0.0

8 years ago

0.1.0

8 years ago

0.0.1

8 years ago