Total Complexity | 5 |
Complexity/F | 1.67 |
Lines of Code | 45 |
Function Count | 3 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | // https://github.com/mqttjs/MQTT.js/issues/647 |
||
3 | let mqtt = require('mqtt') |
||
4 | |||
5 | let username = 'test', |
||
6 | password = 'test', |
||
7 | url = 'mqtt://0.0.0.0', |
||
8 | port = 1883, |
||
9 | topic = 'test' |
||
10 | |||
11 | |||
12 | let options = { |
||
13 | username: username, |
||
14 | password: password, |
||
15 | port: port, |
||
16 | clientId: 'mqtt-listen', |
||
17 | clean: true, |
||
18 | will: { |
||
19 | topic: 'device', |
||
20 | payload: 'pipeline' |
||
21 | } |
||
22 | } |
||
23 | |||
24 | let client = mqtt.connect(url, options) |
||
25 | |||
26 | client.on('connect', function (connection) { |
||
27 | console.log('connection.sessionPresent:', connection.sessionPresent) |
||
|
|||
28 | client.subscribe(topic, {}, function (error, granted) { |
||
29 | if (error) { |
||
30 | return console.error |
||
31 | } |
||
32 | console.log(`Subscribed "${ topic }"`, granted) |
||
33 | }) |
||
34 | |||
35 | }) |
||
36 | |||
37 | client.on('message', function (topic, message) { |
||
38 | console.log('Last id: ' + client.getLastMessageId()) |
||
39 | if (message.toString() !== 'none') { |
||
40 | console.log(message.toString()) |
||
41 | // Delete the retained message |
||
42 | client.publish(topic, 'none', {retain: true}) |
||
43 | } else { |
||
44 | console.log('No message available') |
||
45 | } |
||
46 | client.end() |
||
47 | }) |
||
48 | |||
50 |