@evva/nest-mqtt 2.0.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +25 -25
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -24,37 +24,37 @@ $ nest build
|
|
|
24
24
|
|
|
25
25
|
## Usage
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
MqttModule.forRootAsync({
|
|
29
|
-
imports: [ConfigModule],
|
|
30
|
-
inject: [ConfigService],
|
|
31
|
-
useFactory: (configService: ConfigService) =>
|
|
32
|
-
({
|
|
33
|
-
host: configService.get<string>(BROKER_HOST),
|
|
34
|
-
port: configService.get<Number>(BROKER_PORT, {'infer': true}),
|
|
35
|
-
protocol: configService.get<string>(BROKER_PROTOCOL),
|
|
36
|
-
username: configService.get<string>(BROKER_CLIENT_USER),
|
|
37
|
-
password: configService.get<string>(
|
|
38
|
-
BROKER_CLIENT_PASSWORD,
|
|
39
|
-
),
|
|
40
|
-
share: configService.get<string>(BROKER_SHARED_PREFIX),
|
|
41
|
-
}) as MqttModuleOptions,
|
|
42
|
-
}),
|
|
43
|
-
```
|
|
27
|
+
Import the module wherever needed and inject the `MqttService` into your service for use.
|
|
44
28
|
|
|
45
|
-
When using the ConfigService, make sure that the variables are loaded before accessing them.
|
|
46
|
-
This usually works as follows:
|
|
47
29
|
```ts
|
|
48
|
-
|
|
49
|
-
|
|
30
|
+
@Module({
|
|
31
|
+
imports: [MqttModule],
|
|
32
|
+
})
|
|
33
|
+
export class ExampleModule {
|
|
34
|
+
constructor(private readonly mqttService: MqttService) {}
|
|
50
35
|
|
|
51
|
-
async
|
|
52
|
-
|
|
36
|
+
public async example() {
|
|
37
|
+
try {
|
|
38
|
+
await this.mqttService.connect({
|
|
39
|
+
host: '127.0.0.1',
|
|
40
|
+
protocol: 'mqtt',
|
|
41
|
+
port: 1883,
|
|
42
|
+
username: 'test',
|
|
43
|
+
password: 'test',
|
|
44
|
+
autoSubscribe: true
|
|
45
|
+
})
|
|
46
|
+
} catch (e) {
|
|
47
|
+
console.log(`Failed to connect: ${e}`)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
@Subscribe('exampleTopic')
|
|
52
|
+
public onExampleTopicEvent(@Payload() payload: object) {
|
|
53
|
+
console.log(payload)
|
|
53
54
|
}
|
|
54
55
|
}
|
|
55
56
|
```
|
|
56
|
-
|
|
57
|
-
## Support
|
|
57
|
+
> Tip: Use the `autoSubscribe` flag to automatically subscribe to all @Subscribe() decorators.
|
|
58
58
|
|
|
59
59
|
## Stay in touch
|
|
60
60
|
|