tencent cloud

ドキュメントTDMQ for MQTT

JavaScript SDK

フォーカスモード
フォントサイズ
最終更新日: 2026-04-01 16:37:51
MQTT.js is a JavaScript module that implements MQTT protocol client functionality and can be used in browser and Node.js environments.
Since JavaScript is single-thread, MQTT.js is a fully asynchronous MQTT client. It supports MQTT and MQTT over WebSocket, with different levels of support in execution environments as follows:
Browser environment: MQTT over WebSocket (including WeChat mini program, Alipay mini program, and other custom browser environments).
Node.js environment: MQTT and MQTT over WebSocket.
In different environments, except for a few connection parameters, all other APIs are the same.
Install using npm:
npm i mqtt
Install using CDN (browser):
<script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script>
<script>
// Initialize a global mqtt variable.
console.log(mqtt)
</script>
In a Node.js environment, you can use the command `npm i mqtt -g` to globally install MQTT.js for command line usage.
npm i mqtt -g

mqtt help

MQTT.js command line interface, available commands are:

* publish publish a message to the broker
* subscribe subscribe for updates from the broker
* version the current MQTT.js version
* help help about commands

Launch 'mqtt help [command]' to know more about the commands.

MQTT.Js Usage Example

// const mqtt = require('mqtt')
import mqtt from 'mqtt'

// Connection options
const options = {
clean: true, // true: Clear session, false: Retain session
connectTimeout: 4000, // Timeout period
// Certification information
clientId: '<your-client-id>',
username: '<your-username>',
password: '<your-password>',
}

// Connection string, which specifies the connection method via the protocol
// ws unencrypted WebSocket connection
// wss encrypted WebSocket connection
// mqtt unencrypted TCP connection
// mqtts encrypted TCP connection
// wxs WeChat mini program connection
// alis Alipay mini program connection
const connectUrl = 'wss://mqtt-xxx.mqtt.tencenttdmq.com:8084/mqtt'
const client = mqtt.connect(connectUrl, options)

client.on('reconnect', (error) => {
console.log('Reconnecting:', error)
})

client.on('error', (error) => {
console.log('Connection failure:', error)
})

client.on('message', (topic, message) => {
console.log('received message:', topic, message.toString())
})
Parameter
Description
topic
MQTT level 1 topic, copied from the Topic page on the cluster details page in the console.

connectUrl
Broker connection address, copied from the Basic Information > Access Information section of the target cluster in the console, as shown below. Format: mqtt-xxx-gz.mqtt.qcloud.tencenttdmq.com:1883.

clientId
Client ID, obtained from the Client Management page on the cluster details page in the console.

username
Connection username, copied from the Authentication Management page on the cluster details page in the console.

password
Password matching the connection username, copied from the Authentication Management page on the cluster details page in the console.

MQTT.Js MQTT 5.0 Support

MQTT.js currently has complete support for MQTT 5.0.

ヘルプとサポート

この記事はお役に立ちましたか?

フィードバック