Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MQTTJS TLS support #28341

Open
Vetnametz opened this issue Feb 28, 2025 · 1 comment
Open

MQTTJS TLS support #28341

Vetnametz opened this issue Feb 28, 2025 · 1 comment

Comments

@Vetnametz
Copy link

Vetnametz commented Feb 28, 2025

Version: Deno 2.2.2

Looks like MQTTJS not works properly with Deno and TLS

deno.json

{
  "imports": {
    "mqtt": "npm:[email protected]",
  }
}

mqttClient.ts

import mqtt, { IClientOptions } from 'mqtt';

const options: IClientOptions = {
      protocol: 'mqtt', // mqtts same error
      host: 'someHost',
      port: 8883,
      clientId: crypto.randomUUID(),
      username: Deno.env.get('MQTT_USERNAME'),
      password: Deno.env.get('MQTT_PASSWORD'),
      rejectUnauthorized: false,
      protocolVersion: 5,
      connectTimeout: 10000,
      clean: false,
      reconnectPeriod: 1000,
};

    const mqttCaPath = Deno.env.get('MQTT_CA_PATHS');
    const mqttKeyPath = Deno.env.get('MQTT_KEY_PATH');
    const mqttCertPath = Deno.env.get('MQTT_CERT_PATH');

    if (mqttCaPath && mqttKeyPath && mqttCertPath) {
      const ca = [fs.readFileSync(mqttCaPath as PathOrFileDescriptor)];
      const key = fs.readFileSync(mqttKeyPath as PathOrFileDescriptor);
      const cert = fs.readFileSync(mqttCertPath as PathOrFileDescriptor);

      options.ca = ca;
      options.key = key;
      options.cert = cert;
    }

    this.client = mqtt.connect(options);

PATHS - valid
ca, key, cert - valid

gives such error

ERROR [MQTT] Client:11841d4d-6d97-4367-ac0a-2b2c64c8d01a error: connack timeout
Error: Connection closed
    at file:///.../Library/Caches/deno/npm/registry.npmjs.org/mqtt/5.10.3/build/lib/client.js:730:49
    at Array.forEach (<anonymous>)
    at MqttClient._flushVolatile (file:///.../Library/Caches/deno/npm/registry.npmjs.org/mqtt/5.10.3/build/lib/client.js:727:40)
    at TLSSocket.<anonymous> (file:///.../Library/Caches/deno/npm/registry.npmjs.org/mqtt/5.10.3/build/lib/client.js:269:18)
    at TLSSocket.emit (ext:deno_node/_events.mjs:393:28)
    at node:net:1158:14
    at Array.processTicksAndRejections (ext:deno_node/_next_tick.ts:28:11)
    at eventLoopTick (ext:core/01_core.js:174:29) {
  [stack]: [Getter/Setter],
  [message]: "Connection closed"
}
ERROR Main [WORKER]:mqtt-worker- [MQTT] error: Connection closed

MQTT broker logs

  |   | 2025-02-28T11:40:05: Client <unknown> closed its connection. |  
  |   | 2025-02-28T11:40:05: Client <unknown> closed its connection. |  
  |   | 2025-02-28T11:39:55: New connection from ip:61824 on port 8883. |  
  |   | 2025-02-28T11:39:55: New connection from ip:61824 on port 8883.

Can confirm MQTTJS with same ca, key, cert works properly with Node

@marvinhagemeister
Copy link
Contributor

marvinhagemeister commented Feb 28, 2025

Tip: Single backticks don't work well with multiline code. To format multiple lines of code, use 3 backticks. You can even pass a language specifier like ts, tsx, sh, etc to apply some syntax highlighting.

```ts
myCode()
```

This works everywhere across GitHub and makes issues much more friendly to read. More information on this here: https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/creating-and-highlighting-code-blocks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants