Created
June 28, 2018 10:13
-
-
Save arnaudcourtecuisse/ce7251ab4806d90ea76ccc1d40b29c71 to your computer and use it in GitHub Desktop.
Basic AMQP producer/consumer implementation using `amqplib`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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