tencent cloud

TDMQ for MQTT

Release Notes and Announcements
Release Notes
Product Introduction
TDMQ Product Series Introduction and Selection
What Is TDMQ for MQTT
Scenarios
Technical Architecture
Product series
MQTT Protocol Compatibility Notes
Comparison with Apache
High Availability
Product Constraints and Usage Quota
Basic Concepts
Supported Regions
Billing
Billing Overview
Renewal Instructions
Viewing Consumption Details
Overdue Payment Instructions
Refund
Getting Started
Guide for Getting Started
Preparations
Public Network Access
VPC Network Access
User Guide
Usage Process Guide
Configuring Account Permission
Creating a Cluster
Managing Topic
Connecting to the Cluster
Querying Messages
Managing Client
Managing a Cluster
Viewing Monitoring Metrics and Configuring Alarm Policies
Data Integration
Integrating Data Into SCF
Integrating Data Into CKafka
Integrating Data into RocketMQ
Development Guide
MQTT 5 Advanced Features
Data Plane HTTP API Description
Quota and Flow Control Mechanism Description
Configuring a Custom Domain Name
Configuring SQL Filtering
Configuring Point-to-Point Subscription
MQTT over QUIC
Managing Client Subscription
Message Enhancement Rule
Use Cases
Must-Knows for MQTT Client Development
Observability
Topic and Wildcard Subscriptions
​​API Reference
History
Introduction
API Category
Making API Requests
Cluster APIs
Topic APIs
Authorization Policy APIs
User APIs
Client APIs
Message Enhancement Rule APIs
Message APIs
Data Types
Error Codes
SDK Reference
Access Point Format
Java SDK
C SDK
Javascript/Node.JS/Mini Program
Go SDK
iOS SDK
JavaScript SDK
Dart SDK
Python SDK
.NET
Security and Compliance
Permission Management
FAQs
Related Agreement
Privacy Policy
Data Privacy And Security Agreement
TDMQ for MQTT Service Level Agreement
Contact Us

JavaScript SDK

Focus Mode
Font Size
Last updated: 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.

Help and Support

Was this page helpful?

Help us improve! Rate your documentation experience in 5 mins.

Feedback