Release Notes
npm i mqtt
<script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script><script>// Initialize a global mqtt variable.console.log(mqtt)</script>
npm i mqtt -gmqtt helpMQTT.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 commandsLaunch 'mqtt help [command]' to know more about the commands.
// const mqtt = require('mqtt')import mqtt from 'mqtt'// Connection optionsconst options = {clean: true, // true: Clear session, false: Retain sessionconnectTimeout: 4000, // Timeout period// Certification informationclientId: '<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 connectionconst 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. |
Was this page helpful?
You can also Contact sales or Submit a Ticket for help.
Help us improve! Rate your documentation experience in 5 mins.
Feedback