Passed
Push — master ( d06fb8...9f4cb8 )
by André
03:15 queued 01:29
created

spec/fixtures/mqtt/mqtt-listen.js   A

Complexity

Total Complexity 5
Complexity/F 1.67

Size

Lines of Code 45
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 30
mnd 2
bc 2
fnc 3
dl 0
loc 45
rs 10
bpm 0.6666
cpm 1.6666
noi 4
c 0
b 0
f 0
1
// https://github.com/mqttjs/MQTT.js/issues/647
2
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)
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
28
	client.subscribe(topic, {}, function (error, granted) {
29
		if (error) {
30
			return console.error
31
		}
32
		console.log(`Subscribed "${ topic }"`, granted)
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
33
	})
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
34
35
})
36
37
client.on('message', function (topic, message) {
38
	console.log('Last id: ' + client.getLastMessageId())
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
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
49
50