# Cannot subscribe with node.js

**URL:** https://community.hivemq.com/t/cannot-subscribe-with-node-js/3041
**Category:** HiveMQ Cloud
**Created:** [July 9, 2024, 2:49am UTC](https://community.hivemq.com/t/cannot-subscribe-with-node-js/3041 "2024-07-09T02:49:05Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![asyarif](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@asyarif](https://community.hivemq.com/u/asyarif)
#### Post date: [July 9, 2024, 2:49am UTC](https://community.hivemq.com/t/cannot-subscribe-with-node-js/3041/1 "2024-07-09T02:49:05Z")

</div>

Hello! I am trying to make a school project that requires subscribing to MQTT from my backend (using node.js). this is the code i use to subscribe to a topic in my cluster,

```auto
const dotenv = require('dotenv');
dotenv.config();
const MQTT = require('mqtt');
const fs = require('fs');

const HOST = process.env.SERVERHOSTNAME;
const PORT = 8883;
const CLIENTID = `Traceability-test`;
const CONNECTURL = `mqtts://${HOST}:${PORT}`;
const TOPIC = 'Inkubasi/01A';
const CA_CERT_PATH = './isrgrootx1.pem'; // Path to your Let's Encrypt root CA file

//Encrypt root CA certificate
let caCert;
try {
  caCert = fs.readFileSync(CA_CERT_PATH);
} catch (err) {
  console.error('Error reading CA certificate:', err);
  // Handle error
}

const client = MQTT.connect(CONNECTURL, {
  clientId: CLIENTID,
  clean: true,
  connectTimeout: 7200,
  username: process.env.USERNAME,
  password: process.env.PASSWORD,
  reconnectPeriod: 10000,
  ca: caCert, // Provide the CA certificate to the MQTT client
});

client.on("error",function(error){ console.log("Can't connect"+error)})

client.on('connect', async () => {
  console.log('Connected')
  client.subscribe([TOPIC], () => {
    console.log(`Subscribe to TOPIC '${TOPIC}'`)
  })
})

client.on('message', (TOPIC, payload) => {
    console.log('Received Message:', TOPIC, payload.toString())
})

```

I already added the root CA following instruction from the other discussions. But it keeps saying this error.

```auto
> Can't connectErrorWithReasonCode: Connection refused: Not authorized

```

Any help would be appreciated. Thanks in advance!!

---

<div class="post-metadata">

### Author: ![Diego](https://avatars.discourse-cdn.com/v4/letter/d/f04885/32.png) [@Diego](https://community.hivemq.com/u/Diego)
#### Post date: [July 9, 2024, 8:40am UTC](https://community.hivemq.com/t/cannot-subscribe-with-node-js/3041/2 "2024-07-09T08:40:03Z")

</div>

Hello @asyarif

It appears that your MQTT client code is experiencing authentication issues when connecting to the HiveMQ Cloud broker. This could be due to incorrect credentials. Please review the example provided in our GitHub repository below for guidance.

> **[GitHub - hivemq-cloud/mqtt.js-client-example: Complete example project to get...](https://github.com/hivemq-cloud/mqtt.js-client-example)**
>
> Complete example project to get started with MQTT.js on HiveMQ Cloud - hivemq-cloud/mqtt.js-client-example

Kind regards,  
Diego from HiveMQ Team

---

<div class="post-metadata">

### Author: ![asyarif](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@asyarif](https://community.hivemq.com/u/asyarif)
#### Post date: [July 9, 2024, 12:30pm UTC](https://community.hivemq.com/t/cannot-subscribe-with-node-js/3041/3 "2024-07-09T12:30:05Z")

</div>

thanks for the reply! I have followed the guide, but it still gives an error like this

```auto
Error: Error: getaddrinfo ENOTFOUND 8883

```

\*Edit: It works! there was a little mistake in my code, i accidentally removed the “tls://” before host
