tencent cloud

JavaScript SDK
Last updated:2026-01-30 15:02:26
JavaScript SDK
Last updated: 2026-01-30 15:02:26
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 custom browser environments).
Node.js environment: MQTT, MQTT over WebSocket.
In different environments, except for a few connection parameters, all other APIs are identical.
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>',
}

// concatenated string, specifies the use of connection method via 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
Copy the MQTT level 1 Topic from the Topic Prefix page on the cluster detail page in the console.

connectUrl
The broker connection address can be copied from the Basic infomation > Access information module on the console target cluster. The location is shown below. Format: mqtt-xxx-gz.mqtt.qcloud.tencenttdmq.com:1883.

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

username
Connection Username can be copied from the Authentication Management page on the cluster details page in the console.

password
The password matching the Connection Username can be 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.
Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback