Skip to content

Instantly share code, notes, and snippets.

@arnaudcourtecuisse
Created June 28, 2018 10:13
Show Gist options
  • Save arnaudcourtecuisse/ce7251ab4806d90ea76ccc1d40b29c71 to your computer and use it in GitHub Desktop.
Save arnaudcourtecuisse/ce7251ab4806d90ea76ccc1d40b29c71 to your computer and use it in GitHub Desktop.
Basic AMQP producer/consumer implementation using `amqplib`
const amqp = require('amqplib');
const getChannel = async (uri = 'amqp://localhost') => {
const connection = await amqp.connect(uri);
return connection.createChannel();
};
const postMessages = async (channel, queue) => {
await channel.assertQueue(queue);
setInterval(() => channel.sendToQueue(queue, new Buffer('this is my message to the world')), 1000);
}
const readMessages = async (channel, queue) => {
await channel.assertQueue(queue);
channel.consume(queue, message => console.log(message.content.toString()));
}
getChannel().then(channel => postMessages(channel, 'test')).catch(console.warn);
getChannel().then(channel => readMessages(channel, 'test')).catch(console.warn);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment