0

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Wai Yan Hein
  • 13,651
  • 35
  • 180
  • 372

1 Answers1

1

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.

Justin Bertram
  • 29,372
  • 4
  • 21
  • 43
  • Hi thanks for the answer. I corrected the topic in consumer.js and client_id in both scripts. I also checked the endpoints and I am using the right ports. It still does not work. Could this be some sort of AWS related issues? – Wai Yan Hein Jul 19 '23 at 22:03
  • I also tried with this, `mqtt+ssl://b-xxxxxx-d21c-xxxx-xxxx-eda5de73ea53-1.mq.eu-west-2.amazonaws.com:8883`. still does not work. – Wai Yan Hein Jul 19 '23 at 22:03
  • It's working as expected now actually. I just needed to update the security group. Thanks – Wai Yan Hein Jul 19 '23 at 22:20
  • 1
    If my answer addressed your question please mark it as correct to help other users who encounter similar issues in the future. Thanks! – Justin Bertram Jul 20 '23 at 00:06
  • I upvoted it. It was not the answered but helpful. The issue was AWS related. – Wai Yan Hein Jul 20 '23 at 00:59
  • You asked about how to fix _your code_. The fact that you reused the client ID and did not define the `topic` variable were real problems with your code. Given the information you provided in your question it would be impossible to deduce that there was a problem with the back-end security configuration, and either way that's not what you asked about. Without fixing the code the applications still wouldn't have worked even if the back-end security problem was resolved. – Justin Bertram Jul 20 '23 at 01:58