tencent cloud

消息队列 MQTT 版

动态与公告
新功能发布记录
产品简介
TDMQ 产品系列介绍与选型
什么是消息队列 MQTT 版
应用场景
技术架构
产品系列
MQTT 协议兼容说明
开源对比
高可用
产品约束与使用配额
基本概念
开服地域
购买指南
计费概述
续费说明
查看消费明细
欠费说明
退费说明
快速入门
入门流程指引
准备工作
公网接入
VPC 网络接入
用户指南
使用流程指引
配置账号权限
新建集群
管理 Topic
连接集群
查询消息
管理客户端
管理集群
查看监控和配置告警
数据集成
集成数据到云函数 SCF
集成数据到 CKafka
集成数据到 RocketMQ
开发指南
MQTT 5 高级特性
数据面 HTTP 接口说明
配置自定义域名
配置 SQL 过滤
配置点对点订阅
MQTT over QUIC
管理客户端订阅
消息增强规则
实践教程
MQTT 客户端开发注意事项
可观测能力
Topic 与通配符订阅
API 参考
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 参考
接入点格式
Java SDK
C SDK
Javascript/Node.JS/小程序
Go SDK
iOS SDK
JavaScript SDK
Dart SDK
Python SDK
.NET
安全与合规
权限管理
常见问题
相关协议
隐私协议
数据处理和安全协议
消息队列 MQTT 版服务等级协议
联系我们

接入点格式

聚焦模式
字号
最后更新时间: 2026-01-30 15:23:30

场景说明

本文介绍常见消息队列 MQTT 版的接入点获取方式和各语言 SDK 在不同协议下的 MQTT 接入点格式,帮助您快速在不同开发环境中配置和连接 MQTT 服务,确保设备与云端的安全可靠通信。

接入点获取方式

1. 登录 MQTT 控制台
2. 在左侧导航栏选择资源管理 > 集群管理,选择好地域后,单击目标集群的“ID”,进入基本信息页面。
3. 在接入信息模块,可以获取 MQTT 接入点。


Java SDK

环境准备:
Maven
Gradle
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.2.5</version>
</dependency>

implementation group: 'org.eclipse.paho', name: 'org.eclipse.paho.client.mqttv3', version: '1.2.5'

接入点格式:
TCP
SSL/TLS
WebSocket
WebSocket over SSL
String serverUri = "tcp://mqtt-xxx.mqtt.tencenttdmq.com:1883";
String serverUri = "ssl://mqtt-xxx.mqtt.tencenttdmq.com:8883";
String serverUri = "ws://mqtt-xxx.mqtt.tencenttdmq.com:80/mqtt";
String serverUri = "wss://mqtt-xxx.mqtt.tencenttdmq.com:443/mqtt";

C

环境准备:
git clone https://git.eclipse.org/r/paho/org.eclipse.paho.mqtt.c
make
sudo make install
接入点格式:
/**
* Return code: protocol prefix in serverURI should be:
* @li @em tcp:// or @em mqtt:// - Insecure TCP
* @li @em ssl:// or @em mqtts:// - Encrypted SSL/TLS
* @li @em ws:// - Insecure websockets
* @li @em wss:// - Secure web sockets
*
* The TLS enabled prefixes (ssl, mqtts, wss) are only valid if the TLS
* version of the library is linked with.
*/
TCP
SSL/TLS
WebSocket
WebSocket over SSL
tcp://mqtt-xxx.mqtt.tencenttdmq.com:1883
or
mqtt://mqtt-xxx.mqtt.tencenttdmq.com:1883
ssl://mqtt-xxx.mqtt.tencenttdmq.com:1883
or
mqtts://mqtt-xxx.mqtt.tencenttdmq.com:1883
ws://mqtt-xxx.mqtt.tencenttdmq.com:80/mqtt
wss://mqtt-xxx.mqtt.tencenttdmq.com:443/mqtt

C++

环境准备:
$ git clone https://github.com/eclipse/paho.mqtt.c.git
$ cd paho.mqtt.c
$ git checkout v1.3.13

$ cmake -Bbuild -H. -DPAHO_ENABLE_TESTING=OFF -DPAHO_WITH_SSL=ON -DPAHO_HIGH_PERFORMANCE=ON
$ sudo cmake --build build/ --target install
接入点格式:
The library supports connecting to an MQTT server/broker using TCP, SSL/TLS, and websockets (secure and insecure). This is chosen by the URI supplied to the connect() call. It can be specified as:

"mqtt://<host>:<port>" - TCP, unsecure
"tcp://<host>:<port>" (same)

"mqtts://<host>:<port>" - SSL/TLS
"ssl://<host>:<port>" (same)

"ws://<host>:<port>" - Unsecure websockets
"wss://<host>:<port>" - Secure websockets

The "mqtt://" and "tcp://" schemas are identical. They indicate an insecure connection over TCP. The "mqtt://" variation is new for the library, but becoming more common across different MQTT libraries.

Similarly, the "mqtts://" and "ssl://" schemas are identical. They specify a secure connection over SSL/TLS sockets.

Note that to use any of the secure connect options, "mqtts://, "ssl://", or "wss://" you must compile the library with the `PAHO_WITH_SSL=ON` CMake option to include OpenSSL. In addition, you _must_ specify `ssl_options` when you connect to the broker - i.e. you must add an instance of `ssl_options` to the `connect_options` when calling `connect()`.

Golang

环境准备:
go get github.com/eclipse/paho.mqtt.golang
接入点格式:
TCP
SSL/TLS
import (
"fmt"
mqtt "github.com/eclipse/paho.mqtt.golang"
)

var broker = "mqtt-xxx.mqtt.tencenttdmq.com"
var port = 8883
opts := mqtt.NewClientOptions()
opts.AddBroker(fmt.Sprintf("ssl://%s:%d", broker, port))
opts.SetClientID("golang_mqtt_client")
import (
"fmt"
mqtt "github.com/eclipse/paho.mqtt.golang"
)

var broker = "mqtt-xxx.mqtt.tencenttdmq.com"
var port = 8883
opts := mqtt.NewClientOptions()
opts.AddBroker(fmt.Sprintf("ssl://%s:%d", broker, port))
opts.SetClientID("golang_mqtt_client_ssl")

Rust

环境准备:
cargo add paho-mqtt
接入点格式:
TCP
SSL/TLS
use std::time::Duration;
use anyhow::Context;
use paho_mqtt::{
Client, ConnectOptionsBuilder, CreateOptionsBuilder, DisconnectOptionsBuilder, Message,
};

fn main() -> Result<(), anyhow::Error> {
env_logger::init();
let connect_options = ConnectOptionsBuilder::new_v3()
.connect_timeout(Duration::from_secs(3))
.clean_session(true)
.max_inflight(128)
.user_name("xxx")
.password("XXXX")
.automatic_reconnect(Duration::from_secs(1), Duration::from_secs(3))
.server_uris(&["tcp://mqtt-xxx.mqtt.tencenttdmq.com:1883"])
.finalize();
let client = Client::new(
CreateOptionsBuilder::new_v3()
.client_id("rust-quick-start")
.finalize(),
)
.context("Failed to create client")?;
Ok(())
}
use std::time::Duration;
use anyhow::Context;
use paho_mqtt::{
Client, ConnectOptionsBuilder, CreateOptionsBuilder, DisconnectOptionsBuilder, Message,
SslOptionsBuilder,
};

fn main() -> Result<(), anyhow::Error> {
env_logger::init();
let ssl_opt = SslOptionsBuilder::new().finalize();
let connect_options = ConnectOptionsBuilder::new_v3()
.connect_timeout(Duration::from_secs(3))
.clean_session(true)
.max_inflight(128)
.user_name("xxx")
.password("XXX")
.automatic_reconnect(Duration::from_secs(1), Duration::from_secs(3))
.server_uris(&["ssl://mqtt-xxx.mqtt.tencenttdmq.com:8883"])
.ssl_options(ssl_opt)
.finalize();
let client = Client::new(
CreateOptionsBuilder::new_v3()
.client_id("rust-ssl")
.finalize(),
)
.context("Failed to create client")?;
Ok(())
}

MQTT.js

环境准备:
npm install mqtt
接入点格式:
TCP
SSL/TLS
WebSocket
WebSocket over SSL
import mqtt from 'mqtt';

var opts = {
username: 'my-username',
password: 'my-password'
};
const client = mqtt.connect('mqtt://mqtt-xxx.mqtt.tencenttdmq.com', opts);
import fs from 'fs';
import mqtt from 'mqtt';

const options = {
protocol: 'mqtts',
host: 'mqtt-xxx.mqtt.tencenttdmq.com',
port: 8883,
ca: [fs.readFileSync('/path/to/ca.crt')],
cert: fs.readFileSync('/path/to/client.crt'),
key: fs.readFileSync('/path/to/client.key'),
};
const client = mqtt.connect(options);
import mqtt from 'mqtt';

var opts = {
username: 'my-username',
password: 'my-password'
};
const client = mqtt.connect(‘ws://mqtt-xxx.mqtt.tencenttdmq.com:80/mqtt’, opts);
import mqtt from 'mqtt';

var opts = {
username: 'my-username',
password: 'my-password'
};

const client = mqtt.connect(‘wss://mqtt-xxx.mqtt.tencenttdmq.com:443/mqtt’, opts);

Dart

环境准备:
dart pub add mqtt_client
接入点格式:
import 'dart:async';
import 'dart:io';
import 'package:mqtt_client/mqtt_client.dart';
import 'package:mqtt_client/mqtt_server_client.dart';

final client = MqttServerClient('mqtt-xxx.mqtt.tencenttdmq.com', '1883');

Future<int> main() async {
client.logging(on: true);
client.keepAlivePeriod = 60;
client.onDisconnected = onDisconnected;
client.onConnected = onConnected;
client.pongCallback = pong;

final connMess = MqttConnectMessage()
.withClientIdentifier('dart_client')
.withWillTopic('willtopic')
.withWillMessage('My Will message')
.startClean()
.withWillQos(MqttQos.atLeastOnce);
print('client connecting....');
client.connectionMessage = connMess;

try {
await client.connect();
} on NoConnectionException catch (e) {
print('client exception - $e');
client.disconnect();
} on SocketException catch (e) {
print('socket exception - $e');
client.disconnect();
}

if (client.connectionStatus!.state == MqttConnectionState.connected) {
print('client connected');
} else {
print('client connection failed - disconnecting, status is ${client.connectionStatus}');
client.disconnect();
exit(-1);
}
return 0;
}


/// The unsolicited disconnect callback
void onDisconnected() {
print('OnDisconnected client callback - Client disconnection');
if (client.connectionStatus!.disconnectionOrigin ==
MqttDisconnectionOrigin.solicited) {
print('OnDisconnected callback is solicited, this is correct');
}
exit(-1);
}

/// The successful connect callback
void onConnected() {
print('OnConnected client callback - Client connection was sucessful');
}

/// Pong callback
void pong() {
print('Ping response client callback invoked');
}

Python

环境准备:
pip install paho-mqtt
接入点格式:
import paho.mqtt.client as mqtt
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, reason_code, properties):
print(f"Connected with result code {reason_code}")
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("$SYS/#")
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
mqttc.on_connect = on_connect
mqttc.on_message = on_message
mqttc.connect("mqtt-xxx.mqtt.tencenttdmq.com:1883", 1883, 60)
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
mqttc.loop_forever()


帮助和支持

本页内容是否解决了您的问题?

填写满意度调查问卷,共创更好文档体验。

文档反馈