Skip to content

Commit 58e9077

Browse files
committed
Update packages, add async/await support and improve logs
1 parent 1affb7d commit 58e9077

File tree

3 files changed

+53
-38
lines changed

3 files changed

+53
-38
lines changed

authenticationServer.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ const express = require('express');
22
const bodyParser = require('body-parser');
33
const mqttWildcard = require('mqtt-wildcard');
44
const app = express();
5-
app.use(bodyParser());
5+
app.use(bodyParser.urlencoded({ extended: true }));
6+
app.use(bodyParser.json());
67
const port = 8080;
78

89
const users = [

client.js

+49-35
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,66 @@
11
require('dotenv').config();
22
const mqtt = require('mqtt');
33

4-
if (!process.env.MOSQUITTO_SERVER) {
5-
throw Error('You should first fill the .env-example file and rename it to .env');
6-
}
7-
8-
// Connection to MQTT server
9-
const client = mqtt.connect(
10-
// Note: you can replace 'mqtts' per 'wss' to connect to Mosquitto using websockets (you need to activate the option on Stackhero console and to configure "Security shield" to allow your IPs).
11-
`mqtts://${process.env.MOSQUITTO_SERVER}:${process.env.MOSQUITTO_PORT}`,
12-
{
13-
username: process.env.MOSQUITTO_USERNAME,
14-
password: process.env.MOSQUITTO_PASSWORD,
15-
clean: true
4+
(async () => {
5+
if (!process.env.MOSQUITTO_SERVER) {
6+
throw Error('You should first fill the .env-example file and rename it to .env');
167
}
17-
);
188

9+
let client;
1910

20-
// This callback will be executed when a message is received on topics you subscribed (see client.subscribe below)
21-
client.on('message', (topic, message) => {
22-
// message is Buffer
23-
console.log(`[${topic}]: ${message.toString()}`);
24-
});
11+
// Connect to the MQTT server
12+
console.log('Connecting...');
13+
client = await mqtt.connectAsync(
14+
// Note: you can replace 'mqtts' per 'wss' to connect to Mosquitto using websockets (you need to activate the option on Stackhero console and to configure "Security shield" to allow your IPs).
15+
`mqtts://${process.env.MOSQUITTO_SERVER}:${process.env.MOSQUITTO_PORT}`,
16+
{
17+
username: process.env.MOSQUITTO_USERNAME,
18+
password: process.env.MOSQUITTO_PASSWORD,
19+
clean: true
20+
}
21+
);
22+
console.log('Connected');
2523

2624

27-
// Fired when connection to MQTT is done
28-
client.on('connect', () => {
25+
// This callback will be executed when a message is received on topics you subscribed (see client.subscribe below).
26+
client.on(
27+
'message',
28+
(topic, message) => console.log(`Message received on topic "${topic}": ${message.toString()}`)
29+
);
2930

30-
// We subscribe to the topic "global"
31-
client.subscribe('global', (err, granted) => {
32-
if (err) { throw err; }
3331

34-
if (granted.find(({ qos }) => qos === 128)) {
35-
throw Error(`Permission error when subscribing: ${JSON.stringify(granted)}`);
36-
}
37-
});
32+
// Subscribe to the topic "global"
33+
console.log('Subscribing to "global" topic...');
34+
const granted = await client.subscribeAsync('global');
35+
if (granted.find(({ qos }) => qos === 128)) {
36+
throw Error(`Permission error when subscribing: ${JSON.stringify(granted)}`);
37+
}
38+
console.log('Subscribed to "global" topic');
3839

3940

40-
// We publish to the topic "global"
41-
client.publish('global', 'Hello everyone!', { qos: 2 }, err => {
42-
if (err) { throw err; }
43-
});
41+
// Publish to the topic "global"
42+
console.log('Publishing to "global" topic...');
43+
await client.publishAsync(
44+
'global',
45+
'Hello everyone!',
46+
{ qos: 2 }
47+
);
48+
console.log('Published to "global" topic');
4449

4550

46-
// We publish to as user topic
47-
client.publish('users/testUser/sensor1', '123', { qos: 2 }, err => {
48-
if (err) { throw err; }
49-
});
51+
// Publish to a user topic
52+
console.log('Publishing to "users/testUser/sensor1" topic...');
53+
await client.publishAsync(
54+
'users/testUser/sensor1',
55+
'123',
56+
{ qos: 2 }
57+
);
58+
console.log('Published to "users/testUser/sensor1" topic');
5059

5160
// Note: we can publish to a topic without "write" rights. MQTT will ignore the message without informing us.
61+
})().catch(error => {
62+
console.error('');
63+
console.error('🐞 An error occurred!');
64+
console.error(error);
65+
process.exit(1);
5266
});

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
},
1010
"dependencies": {
1111
"body-parser": "^1.18.3",
12-
"dotenv": "^6.2.0",
12+
"dotenv": "^16.4.5",
1313
"express": "^4.16.4",
14-
"mqtt": "^2.18.8",
14+
"mqtt": "^5.10.1",
1515
"mqtt-wildcard": "^3.0.9"
1616
}
1717
}

0 commit comments

Comments
 (0)