当前位置:网站首页>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 .
边栏推荐
猜你喜欢

【一花一世界-郑一教授-繁简之道】可解释神经网络

JSON 文件存储

RGB-T追踪——【多模态融合】Visible-Thermal UAV Tracking: A Large-Scale Benchmark and New Baseline

51单片机存储篇:EEPROM(I2C)
![Magic brace- [group theory] [Burnside lemma] [matrix fast power]](/img/cf/606d1bc7cd877771afbdd7640b718c.png)
Magic brace- [group theory] [Burnside lemma] [matrix fast power]

站在大佬的肩膀上,你可以看的更远

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

AMQ streams (1) of openshift 4 - multiple consumers receive data from partition

C language array pointer and pointer array discrimination, analysis of memory leakage
![[advanced drawing of single cell] 07. Display of KEGG enrichment results](/img/77/0a9006743734c1993785d087f957f0.png)
[advanced drawing of single cell] 07. Display of KEGG enrichment results
随机推荐
力扣题(1)—— 两数之和
2022年起重机司机(限桥式起重机)考试题库及模拟考试
c# 有符号和无符号字节变量
使用 GBase C API 执行存储过程是怎样的?
IDC脚本文件运行
Setting of parameter configuration tool for wireless vibrating wire collector
Leetcode 452. minimum number of arrows to burst balloons (medium)
Design for failure常见的12种设计思想
Solution and implementation of APP accelerating reading and displaying IPFs pictures
Feign调用异常[Running, pool size = 10, active threads = 10, queued tasks = 0, completed tasks = n]
LeetCode_406_根据身高重建队列
2022 safety officer-c certificate special operation certificate examination question bank and answers
Magic Bracelet-【群论】【Burnside引理】【矩阵快速幂】
oracle 创建用户且只有查询权限
ES查询索引字段的分词结果
Sentry log management system installation and use tutorial
个人博客小程序
Data analysis interview question summary
5 运算符、表达式和语句
CakePHP 4.4.3 发布,PHP 快速开发框架