I am testing out Amazon MQ with the ActiveMQ "engine" using Node.js and MQTT, but it does not seems to be working as expected. This is what I have done so far.
I created in the AWS console choosing the following options
- Apache ActiveMQ
- Active/ stand-by broker
- mq.t2.micro
The broker was created successfully. I now have all the connection details.
I am trying to test it locally creating 2 Node.js files producer.js
and consumer.js
. While the consumer.js
is up and running, when I run producer.js
script, consumer should receive the message.
This is my consumer.js
const mqtt = require('mqtt')
const config = {
username: `myusernmae`,
password: `mypassword`,
mqEndpoint: `wss://b-xxxxxx-d21c-xxxx-xxxx-xxxxxxxxxxxxx-1.mq.eu-west-2.amazonaws.com:61619`, // same endpoint used by producer.js - I got this from WSS section of connections section of the broker
clientId: 'my_client_id',
}
const client = mqtt.connect(config.mqEndpoint, {
username: config.username,
password: config.password,
clientId: config.clientId,
});
// Once connected subscribe to the topic
client.on('connect', function() {
console.log("connected")
client.subscribe(topic, function (err) {
if(err) console.log(err)
})
})
client.on('error', function (error) {
console.log("error")
console.log(error)
})
// Log messages
client.on('message', function (topic, message) {
console.log(`message received on ${topic}: ${message.toString()}`)
})
console.log("Consumer script has been run");
This is my producer.js
:
const mqtt = require('mqtt')
const config = {
username: `myusernmae`,
password: `mypassword`,
host: `wss://b-xxxxxx-d21c-xxxx-xxxx-xxxxxxxxxxxxx-1.mq.eu-west-2.amazonaws.com`,// same endpoint used by consumer.js - I got this from WSS section of connections section of the broker
clientId: 'my_client_id',
port: 61619,
topic: `some/topic`,
}
let client = mqtt.connect(config.host, {
username: config.username,
password: config.password,
clientId: config.clientId,
})
let message = JSON.stringify({
message: "Hello from Lambda"
})
client.on('connect', function() {
client.publish(config.topic, message)
client.end()
})
console.log("Product script has been run");
First I run the consumer: node consumer.js
.
After that I run: node producer.js
.
I am not seeing anything in the console/terminal. When I run node consumer.js
I could only see Consumer script has been run
in the terminal.
Also when I run node producer.js
I only saw Producer script has been run
. It seems like the connection was not formed.
What is wrong with my code/ configuration and how can I fix it?