Repository navigation
Expand file tree
/
Copy pathexample.js
More file actions
46 lines (41 loc) · 1.55 KB
/
Copy pathexample.js
File metadata and controls
46 lines (41 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const mqtt = require('mqtt');
const test_topic = 'test'
const randomstring = (Math.random() + 1).toString(36).substring(6)
const client = mqtt.connect('mqtts://mqtt.flespi.io:8883', {
// The random string is needed when you try to run 2 clients in parallel,
// because each client must have a UNIQUE client_id
clientId: 'flespi-examples-mqtt-client-nodejs_' + randomstring,
// see https://flespi.com/kb/tokens-access-keys-to-flespi-platform to read about flespi tokens
username: `FlespiToken ${process.env.FlespiToken}`,
protocolVersion: 5,
clean: false,
properties: {
sessionExpiryInterval: 60 // this setting is required when clean:false
}
});
console.log('mqtt client created, connecting...');
client.on('connect', () => {
console.log('connected, subscribing to "test" topic...');
client.subscribe(test_topic, {qos: 1}, (err) => {
if (err) {
console.log('failed to subscribe to topic "test":', err);
return;
}
console.log('subscribed to "test" topic, publishing message...');
client.publish(test_topic, 'hello from flespi mqtt client example script!', {qos: 1});
});
});
client.on('message', (topic, msg, packet) => {
console.log(`received ${packet.retain ? 'retained' : 'realtime'} message in topic "${topic}": "${msg.toString('utf8')}"`);
console.log('disconnecting...');
if (!packet.retain) {
client.end();
}
});
client.on('close', () => {
console.log('disconnected');
})
client.on('error', (err) => {
console.log('mqtt client error:', err);
client.end(true) // force disconnect and stop the script
});