daily pastebin goal
10%
SHARE
TWEET

Untitled

a guest Dec 15th, 2018 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const amqp = require('amqplib')
  2.  
  3. const open = amqp.connect('amqp://localhost')
  4.  
  5. const connect = (url = 'amqp://localhost') => {
  6.   return new Promise((resolve, reject) => {
  7.     amqp.connect(url)
  8.       .then(conn => resolve(conn))
  9.       .catch(err => reject(err))
  10.   })
  11. }
  12.  
  13. const createChannel = conn => {
  14.   return new Promise((resolve, reject) => {
  15.     conn.createChannel()
  16.       .then(channel => resolve(channel))
  17.       .catch(err => reject(err))
  18.   })
  19. }
  20.  
  21.  
  22. const channelAssertQueue = (channel, queueName) => {
  23.   return new Promise((resolve, reject) => {
  24.     channel.assertQueue(queueName)
  25.       .then(asserted => resolve(channel))
  26.       .catch(err => reject(err))
  27.   })
  28. }
  29.  
  30. const sendToQueue = (channel, queueName, buffer) =>
  31.   channel.sendToQueue(queueName, buffer)
  32.  
  33.  
  34.  
  35. const connection = async (queueName = 'msg.*') => {
  36.   const conn = await connect()
  37.   const channel = await createChannel(conn)
  38.   const assertedChannelToQueue = await channelAssertQueue(channel, queueName)
  39.   return channel
  40. }
  41.  
  42. module.exports = connection
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top