当前位置:网站首页>MQTT.js 入门教程:学习笔记
MQTT.js 入门教程:学习笔记
2022-07-28 08:46:00 【LIZHUOLONG1】
一、创建文件:【mqtt.js 客户端】
1、新建文件夹 mqtt_pure,建立 mqtt.js 文件;
2、在 VSCode 中打开,新开一个 终端;
3、终端中输入 npm install mqtt --save
4、写入代码:
const mqtt = require('mqtt')
const options = {
// Clean session
clean: true,
connectTimeout: 4000,
// Auth
clientId: 'emqx_test_lzl_mqttjs',
username: 'emqx_test',
password: 'emqx_test',
}
// 确保TCP Port 端口正确
const client = mqtt.connect('mqtt://broker.emqx.io:1883', options)
client.on('connect', function () {
console.log('Connected')
// 订阅到主题后,只触发一次。
client.subscribe('testtopic/lzl-MQTTX', function (err) {
if (!err) {
console.log("subscribe success!!!!")
//发布主题testtopic/lzl-mqttjs
client.publish('testtopic/lzl-mqttjs', 'Hello, 我是mqttjs客户端消息。通过主题【testtopic/lzl-mqttjs】发布而来。 from: client.subscribe')
}
})
}),
// 只要接收到客户端消息,就会触发。
client.on('message', function (topic, message) {
console.log(message.toString())
client.publish('testtopic/lzl-mqttjs', 'Hello, 我是mqttjs客户端消息。 通过主题【testtopic/lzl-mqttjs】发布而来。from: client.on')
}),
client.on('close', function () {
console.log('Disconnected');
console.log('-------------------')
})
- 代码目录:

5、终端输入node mqtt.js运行代码。
二、笔记:
2.1、【mqtt.js客户端】与 【MQTTX 客户端】
(1)两个客户端(mqtt.js MQTTX)连接到同一个服务器。
(2)clientID:全局唯一。如果不同的设备使用相同的clientID同时连接物联网平台,那么先连接的那个设备会被强制断开。
(3)【MQTTX 客户端】的 clientID,和【mqtt.js客户端】的 clientID 不能相同,服务器连接的客户端clientID必须唯一,不然就会挤掉其他的客户端。
- 笔者设置【MQTTX 客户端】的
clientID:emqx_test_lzl_MQTTX。
注意:得使用外网访问服务器,公司内网会有墙隔断。 - 笔者设置【mqtt.js客户端】的
clientId: 'emqx_test_lzl_mqttjs'

2.2、消息订阅
1、运行node mqtt.js,在【mqttx.js 客户端】订阅 【MQTTX客户端】的消息:

2、命令行订阅消息
mqtt sub -t 'testtopic/#' -h 'broker.emqx.io' -v // 连接到 broker.emqx.io 并订阅 testtopic/# 主题;
注释:
1、testtopic/# 代表【mqtt.js客户端】订阅所有testtopic/话题下的内容。
2、testtopic/lzl-MQTTX 代表【mqtt.js客户端】只接收我的【MQTTX客户端】发布的话题内容。
边栏推荐
- 【vscode】vscode使用
- 【JVM】JVM表示浮点数
- js数组去重,id相同对某值相加合并
- 【高数】高数平面立体几何
- Informatics Olympiad all in one 1617: circle game | 1875: [13noip improvement group] circle game | Luogu p1965 [noip2013 improvement group] circle game
- Mongodb (compare relational database, cloud database, common command line, tutorial)
- 一年涨薪三次背后的秘密
- 训练一个自己的分类 | 【包教包会,数据都准备好了】
- DN-DETR 论文精度,并解析其模型结构 & 2022年CVPR论文
- Machine learning: self paced and fine tuning
猜你喜欢

Sentinel
![[592. Fraction addition and subtraction]](/img/3a/1a76a8acd60a6d45eebed612fd3971.png)
[592. Fraction addition and subtraction]

蓝牙技术|2025年北京充电桩总规模达70万个,聊聊蓝牙与充电桩的不解之缘

网络层的IP协议

Sentinel

NPM and yarn use (official website, installation, command line, uploading your own package, detailed explanation of package version number, updating and uninstalling package, viewing all versions, equ

一年涨薪三次背后的秘密

Introduction to official account

Overview of head pose estimation

【vscode】vscode使用
随机推荐
01 tensorflow calculation model (I) - calculation diagram
Kubernetes cluster configuration serviceaccount
【高数】高数平面立体几何
Force deduction question (1) -- sum of two numbers
Starfish Os打造的元宇宙生态,跟MetaBell的合作只是开始
IDC脚本文件运行
Magic brace- [group theory] [Burnside lemma] [matrix fast power]
剑指offer
Go synergy
IP protocol of network layer
Sentinel
golang升级到1.18.4版本 遇到的问题
推荐一个摆脱变量名纠结的神器和批量修改文件名方法
侯捷STL标准库和泛型编程
shell 实现harbor v1/v2的备份/恢复/迁移等功能
如何在多线程环境下使用 GBase C API ?
【单细胞高级绘图】07.KEGG富集结果展示
Bluetooth technology | it is reported that apple, meta and other manufacturers will promote new wearable devices, and Bluetooth will help the development of intelligent wearable devices
c语言数组指针和指针数组辨析,浅析内存泄漏
Setting of parameter configuration tool for wireless vibrating wire collector