0

This is my code:

const amqp = require('amqplib');

amqp.connect().then((conn) => {
  var ok = conn.createChannel();
  ok = ok.then((ch) => {
    ch.assertQueue('myFirstQueue', {
       arguments: {
         messageTtl: 1000
       }
     })
     .then(response => {
       let q = response;
       let msg = 'hello';
       let message = new Buffer(JSON.stringify(msg).toString('base64'));
       ch.sendToQueue(q.queue, message);
       return ok;
     })
     .catch(error => {
       return error;
     })
  });
});

What I expect is that the message expires in a second. When I run my consumer in a few seconds, the message arrives!

Reading the documentation, I understand that I simply have to add arguments in options and pass messageTtl in miliseconds.

What I'm doing wrong?

emilioriosvz
  • 1,629
  • 1
  • 19
  • 30
  • Does your queue already exist? If yes, `assertQueue` would just get that one, not create a new one, and then your ttl wouldn't be set. – Zlatko Jan 09 '17 at 10:51
  • The queue already exist, after your comment I tried to create a new queue `ch.assertQueue('newQueueNeverUsed')` and the behavior is the same. There is another way to do that? – emilioriosvz Jan 09 '17 at 11:54
  • Not sure, I didn't use this lib but rabbit.js directly. – Zlatko Jan 09 '17 at 11:59

1 Answers1

1

You have to pass it as "x-message-ttl" in arguments. Otherwise you can pass "messageTtl" in options. You can check it in their library on github. https://github.com/squaremo/amqp.node/blob/master/lib/api_args.js

Shubham
  • 31
  • 5