当前位置:网站首页>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 .
边栏推荐
- Data analysis interview question summary
- Activiti启报错: Cannot create PoolableConnectionFactory (Could not create connection to database server
- 力扣题(1)—— 两数之和
- MQTT.js 入门教程:学习笔记
- IntelliJ IDEA 关联数据库
- F - jealous two-dimensional reverse order pair
- AMQ streams (1) of openshift 4 - multiple consumers receive data from partition
- App加速读取显示IPFS图片的解决方案和实现
- CakePHP 4.4.3 发布,PHP 快速开发框架
- oracle 创建用户且只有查询权限
猜你喜欢

Modify virtual machine IP address

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

台大林轩田《机器学习基石》习题解答和代码实现 | 【你值得拥有】
![[592. Fraction addition and subtraction]](/img/3a/1a76a8acd60a6d45eebed612fd3971.png)
[592. Fraction addition and subtraction]

How to obtain the subordinate / annotation information of KEGG channel

2022高压电工考试模拟100题及模拟考试

MQTT.js 入门教程:学习笔记

Vs2015 use dumpbin to view the exported function symbols of the library

12 common design ideas of design for failure

The chess robot pinched the finger of a 7-year-old boy, and the pot of a software testing engineer? I smiled.
随机推荐
canvas常用原型方法及绘制图片应用
Solution and implementation of APP accelerating reading and displaying IPFs pictures
Title and answer of work permit for safety management personnel of hazardous chemical business units in 2022
JSON 文件存储
C#简单调用FMU ,进行仿真计算
01-TensorFlow计算模型(一)——计算图
golang 协程的实现原理
【vscode】vscode使用
Data analysis interview question summary
中国地图省>市>级>区>镇>村5级联动下载【2019和2021】
Sentry log management system installation and use tutorial
Principle of line of sight tracking and explanation of the paper
2022高压电工考试模拟100题及模拟考试
MySQL 8.0.30 GA
Learn to draw with nature communications -- complex violin drawing
修改虚拟机IP地址
Openshift 4 - use verticalpodautoscaler to optimize application resource request and limit
个人博客小程序
Introduction to official account
MDM数据质量应用说明