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
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?
I tested this locally with ActiveMQ Artemis, and the first thing I had to change was the connection URL. I tested with both ws://localhost:1883
and mqtt://localhost:1883
and either one resulted in a successful connection.
However, the next problem with your two clients is that they are both using the same client ID (i.e. my_client_id
). Only one client at a time can use a particular client ID. What's happening is that after the consumer connects with my_client_id
the producer also connects with my_client_id
which disconnects the consumer. Then the producer sends a message and disconnects at which point the consumer reconnects. However, since the consumer wasn't connected when the message was sent it doesn't receive it.
Also, you need to define topic
in your consumer.js
.
Lastly, you should confirm that you're using the right port. Currently you're attempting to connect to port 61619
, but the default MQTT port is 1883
for non-SSL and 8883
for SSL.