当前位置:网站首页>MQTT. JS introductory tutorial: learning notes
MQTT. JS introductory tutorial: learning notes
2022-07-28 09:21:00 【LIZHUOLONG1】
One 、 create a file :【mqtt.js client 】
1、 New folder mqtt_pure, establish mqtt.js file ;
2、 stay VSCode Open in , Open a new one terminal ;
3、 Input... In the terminal npm install mqtt --save
4、 Write code :
const mqtt = require('mqtt')
const options = {
// Clean session
clean: true,
connectTimeout: 4000,
// Auth
clientId: 'emqx_test_lzl_mqttjs',
username: 'emqx_test',
password: 'emqx_test',
}
// Make sure TCP Port The port is correct
const client = mqtt.connect('mqtt://broker.emqx.io:1883', options)
client.on('connect', function () {
console.log('Connected')
// After subscribing to the topic , Trigger only once .
client.subscribe('testtopic/lzl-MQTTX', function (err) {
if (!err) {
console.log("subscribe success!!!!")
// Release theme testtopic/lzl-mqttjs
client.publish('testtopic/lzl-mqttjs', 'Hello, I am a mqttjs Client message . Through the theme 【testtopic/lzl-mqttjs】 Released from . from: client.subscribe')
}
})
}),
// As long as the client message is received , It will trigger .
client.on('message', function (topic, message) {
console.log(message.toString())
client.publish('testtopic/lzl-mqttjs', 'Hello, I am a mqttjs Client message . Through the theme 【testtopic/lzl-mqttjs】 Released from .from: client.on')
}),
client.on('close', function () {
console.log('Disconnected');
console.log('-------------------')
})
- Code directory :

5、 The input terminalnode mqtt.jsRun code .
Two 、 note :
2.1、【mqtt.js client 】 And 【MQTTX client 】
(1) Two clients (mqtt.js MQTTX) Connect to the same server .
(2)clientID: Globally unique . If different devices use the same clientID At the same time, connect to the Internet of things platform , Then the device connected first will be forcibly disconnected .
(3)【MQTTX client 】 Of clientID, and 【mqtt.js client 】 Of clientID Cannot be the same , The client connected to the server clientID Must be unique , Otherwise, it will crowd out other clients .
- I set up 【MQTTX client 】 Of
clientID:emqx_test_lzl_MQTTX.
Be careful : You have to use the Internet to access the server , There will be a wall partition in the company's intranet . - I set up 【mqtt.js client 】 Of
clientId: 'emqx_test_lzl_mqttjs'

2.2、 News subscription
1、 function node mqtt.js, stay 【mqttx.js client 】 subscribe 【MQTTX client 】 The news of :

2、 Command line subscription messages
mqtt sub -t 'testtopic/#' -h 'broker.emqx.io' -v // Connect to broker.emqx.io And subscribe to testtopic/# The theme ;
notes :
1、testtopic/# representative 【mqtt.js client 】 Subscribe to all testtopic/ Content under the topic .
2、testtopic/lzl-MQTTX representative 【mqtt.js client 】 Only accept mine 【MQTTX client 】 Published topic content .
边栏推荐
- 10、学习MySQL LIKE 子句
- Face warp - hand tear code
- F - jealous two-dimensional reverse order pair
- MDM data quality application description
- CSV file storage
- 12 common design ideas of design for failure
- 【一花一世界-郑一教授-繁简之道】可解释神经网络
- Train your own classification [Bao Jiaobao, the data are ready]
- 【解决】ERROR in [eslint] ESLint is not a constructor
- JSON 文件存储
猜你喜欢

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

阿里云服务器搭建和宝塔面板连接

个人博客小程序

2022 high voltage electrician examination simulated 100 questions and simulated examination

Alibaba cloud server setup and pagoda panel connection

【英语考研词汇训练营】Day 15 —— analyst,general,avoid,surveillance,compared

Dapp安全总结与典型安全事件分析

2022年安全员-B证考试模拟100题及答案

Design for failure常见的12种设计思想

【SwinTransformer源码阅读二】Window Attention和Shifted Window Attention部分
随机推荐
2022 safety officer-b certificate examination simulated 100 questions and answers
[advanced drawing of single cell] 07. Display of KEGG enrichment results
Bluetooth technology | the total scale of charging piles in Beijing will reach 700000 in 2025. Talk about the indissoluble relationship between Bluetooth and charging piles
js数组去重,id相同对某值相加合并
51单片机存储篇:EEPROM(I2C)
7 C control statements: branches and jumps
Mysql5.7.38 start keepalived in the container
【vscode】vscode使用
Deconstruction assignment of ES6 variables
Realize batch data enhancement | use of keras imagedatagenerator
修改虚拟机IP地址
Train your own classification [Bao Jiaobao, the data are ready]
2022 safety officer-c certificate special operation certificate examination question bank and answers
opencv4.60版本安装和配置
F - Jealous Two-二维逆序对
1.5 merge\rebase\revert\stash\branch
Title and answer of work permit for safety management personnel of hazardous chemical business units in 2022
TXT text file storage
QT基础练手小程序-简单计算器设计(附带源码,解析)
【C语言】详解顺序表(SeqList)