I have been trying to consume a Rabbit MQ with my frontend app with Nuxjs. Am using the code below to define my connection to the backend:
var amqp = require('amqplib')
var EventEmitter = require('events').EventEmitter
var utils = require('util')
var priv = new Map()
function RabbitMQConsumer(opts) {
priv.set(this, opts)
}
utils.inherits(RabbitMQConsumer, EventEmitter)
if(!process.server){
RabbitMQConsumer.prototype.consume = function(cb) {
var self = this
var config = priv.get(self)
var openConnection = function() {
amqp.connect('amqp://****:****@**.***.***.*:****/')
.then(function(conn) {
process.once('SIGINT', function() {
conn.close()
})
conn.on('error', function(err) {
console.error(err.message)
openConnection()
})
conn.createChannel().then(function(ch) {
ch.assertExchange(
config.exchangeName,
config.exchangeType,
config.exchangeOptions
)
.then(function() {
return ch.assertQueue(config.queueName, config.queueOptions)
})
.then(function(queueData) {
ch.bindQueue(
queueData.queue,
config.exchangeName,
config.routingKey
)
return data.queue
})
.then(function(queueName) {
ch.consume(queueName, consumeMessage)
})
.then(function() {
console.log(' [*] Waiting for messages.')
})
function consumeMessage(msg) {
self.emit('received', msg.content.toString())
ch.ack(msg)
}
})
})
.catch(err => {
console.log(err)
})
}
openConnection()
}
}
var configuration = {
userName: '*****',
password: '*****',
host: '*****@**.***.***.*',
port: 5671,
virtualHost: '/',
exchangeName: 'home_exchange',
exchangeType: 'topic',
exchangeOptions: {
durable: true,
internal: false,
autoDelete: false
},
queueName: 'oseni_queue',
queueOptions: {
exclusive: false,
durable: true,
autoDelete: false
},
routingKey: 'fortunebet_routingkey'
}
var consumer = new RabbitMQConsumer(configuration)
consumer.on('received', function(messageText) {
console.log('Message received:', messageText)
})
consumer.consume()
After npm installed amqplib and using the code above am getting error below in my browser console:
TypeError: __webpack_require__(...).connect is not a function
at connect (vendors.app.js:2095)
at vendors.app.js:300
at Object.connect (vendors.app.js:299)
at openConnection (app.js:13816)
at RabbitMQConsumer.consume (app.js:13846)
at VueComponent.mounted (app.js:13875)
at invokeWithErrorHandling (commons.app.js:13938)
at callHook (commons.app.js:16292)
at Object.insert (commons.app.js:15221)
at invokeInsertHook (commons.app.js:18415)
at Vue.patch [as __patch__] (commons.app.js:18563)
at Vue._update (commons.app.js:16018)
at Vue.updateComponent (commons.app.js:16133)
at Watcher.get (commons.app.js:16552)
Despite the error above we are getting the queue name being created at the backend but we can't message on the frontend when messages are added to the queue.
Kindly help see into this.
Am using Nuxjs for my frontend and .Net for the backend.