Skip to content

Instantly share code, notes, and snippets.

@boogie
Created May 21, 2017 07:43
Show Gist options
  • Save boogie/79dc497f0b9fe80fad0aa0ef3ef52d98 to your computer and use it in GitHub Desktop.
Save boogie/79dc497f0b9fe80fad0aa0ef3ef52d98 to your computer and use it in GitHub Desktop.
WRONG implementation: RabbitMQ client closes the connection after 2 secs
#!/usr/bin/env node
'use strict';
const amqp = require('amqplib');
const sleep = async (ms) => new Promise(resolve => setTimeout(resolve, ms));
(async () => {
const conn = await amqp.connect('amqp://localhost:9520');
process.once('SIGINT', () => conn.close());
const ch = await conn.createChannel();
await ch.assertQueue('hello', { durable: false });
await ch.prefetch(1);
await ch.consume('hello', async (msg) => {
console.log(" [x] Received '%s'", msg.content.toString());
await sleep(500);
ch.ack(msg);
});
setTimeout(()=>{
ch.close();
console.log(' [*] Finished');
}, 2000);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment