@fetaoily/nest-mqtt 1.0.0 → 1.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 +40 -31
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,12 +6,15 @@ A MQTT module for Nest.js. Compatible with emqtt.
|
|
|
6
6
|
|
|
7
7
|
## Installation
|
|
8
8
|
|
|
9
|
-
> ⚠️ After version 0.2.0, `nest-mqtt` make a breaking change. User should add additional `mqtt` package manual.
|
|
10
|
-
|
|
11
9
|
```bash
|
|
12
10
|
$ npm install nest-mqtt mqtt --save
|
|
13
11
|
```
|
|
14
12
|
|
|
13
|
+
or
|
|
14
|
+
```bash
|
|
15
|
+
$ yarn add nest-mqtt mqtt
|
|
16
|
+
```
|
|
17
|
+
|
|
15
18
|
## Usage
|
|
16
19
|
|
|
17
20
|
### Import
|
|
@@ -22,52 +25,53 @@ You can import with configuration
|
|
|
22
25
|
|
|
23
26
|
```typescript
|
|
24
27
|
// app.module.ts
|
|
25
|
-
import {
|
|
26
|
-
import {
|
|
28
|
+
import {Module} from '@nestjs/common';
|
|
29
|
+
import {MqttModule} from '@fetaoily/nest-mqtt';
|
|
27
30
|
|
|
28
31
|
@Module({
|
|
29
32
|
imports: [MqttModule.forRoot(options)]
|
|
30
33
|
})
|
|
31
|
-
export class AppModule {
|
|
34
|
+
export class AppModule {
|
|
35
|
+
}
|
|
32
36
|
```
|
|
33
37
|
|
|
34
38
|
or use async import method
|
|
35
39
|
|
|
36
40
|
```typescript
|
|
37
41
|
// app.module.ts
|
|
38
|
-
import {
|
|
39
|
-
import {
|
|
42
|
+
import {Module} from '@nestjs/common';
|
|
43
|
+
import {MqttModule} from '@fetaoily/nest-mqtt';
|
|
40
44
|
|
|
41
45
|
@Module({
|
|
42
46
|
imports: [MqttModule.forRootAsync({
|
|
43
47
|
useFactory: () => options,
|
|
44
48
|
})]
|
|
45
49
|
})
|
|
46
|
-
export class AppModule {
|
|
50
|
+
export class AppModule {
|
|
51
|
+
}
|
|
47
52
|
```
|
|
48
53
|
|
|
49
|
-
|
|
50
54
|
### Subscribe
|
|
51
55
|
|
|
52
56
|
You can define any subscriber or consumer in any provider. For example,
|
|
53
57
|
|
|
54
58
|
```typescript
|
|
55
|
-
import {
|
|
56
|
-
import {
|
|
59
|
+
import {Injectable} from '@nestjs/common';
|
|
60
|
+
import {Subscribe, Payload, Topic} from '@fetaoily/nest-mqtt';
|
|
57
61
|
|
|
58
62
|
@Injectable()
|
|
59
63
|
export class TestService {
|
|
60
64
|
@Subscribe('test')
|
|
61
65
|
test() {
|
|
62
|
-
|
|
66
|
+
|
|
63
67
|
}
|
|
64
|
-
|
|
68
|
+
|
|
65
69
|
@Subscribe({
|
|
66
70
|
topic: 'test2',
|
|
67
71
|
transform: payload => payload.toString(),
|
|
68
72
|
})
|
|
69
73
|
test2() {
|
|
70
|
-
|
|
74
|
+
|
|
71
75
|
}
|
|
72
76
|
}
|
|
73
77
|
```
|
|
@@ -75,8 +79,8 @@ export class TestService {
|
|
|
75
79
|
Also, you can inject parameter with decorator:
|
|
76
80
|
|
|
77
81
|
```typescript
|
|
78
|
-
import {
|
|
79
|
-
import {
|
|
82
|
+
import {Injectable} from '@nestjs/common';
|
|
83
|
+
import {Subscribe, Payload} from '@fetaoily/nest-mqtt';
|
|
80
84
|
|
|
81
85
|
@Injectable()
|
|
82
86
|
export class TestService {
|
|
@@ -105,21 +109,22 @@ Get the raw packet of incoming message.
|
|
|
105
109
|
|
|
106
110
|
Get the wildcard part of topic. It will return an array of string which extract from topic. For example:
|
|
107
111
|
|
|
108
|
-
When subscribe the topic "test/+/test/+" and incoming topic is "test/1/test/2", you will get the array `["1", "2"]`.
|
|
112
|
+
When subscribe the topic "test/+/test/+" and incoming topic is "test/1/test/2", you will get the array `["1", "2"]`.
|
|
109
113
|
|
|
110
114
|
### Publish
|
|
111
115
|
|
|
112
116
|
Nest-mqtt wrap some functions with `Promise` and provide a provider.
|
|
113
117
|
|
|
114
118
|
```typescript
|
|
115
|
-
import {
|
|
116
|
-
import {
|
|
119
|
+
import {Inject, Injectable} from '@nestjs/common';
|
|
120
|
+
import {MqttService} from '@fetaoily/nest-mqtt';
|
|
117
121
|
|
|
118
122
|
@Injectable()
|
|
119
123
|
export class TestService {
|
|
120
124
|
constructor(
|
|
121
125
|
@Inject(MqttService) private readonly mqttService: MqttService,
|
|
122
|
-
) {
|
|
126
|
+
) {
|
|
127
|
+
}
|
|
123
128
|
|
|
124
129
|
async testPublish() {
|
|
125
130
|
this.mqttService.publish('topic', {
|
|
@@ -136,12 +141,13 @@ nest-mqtt support emq shared subscription
|
|
|
136
141
|
|
|
137
142
|
- Global mode
|
|
138
143
|
|
|
139
|
-
Module options support queue and share property for globally converting all topic to shared topic except configured in
|
|
144
|
+
Module options support queue and share property for globally converting all topic to shared topic except configured in
|
|
145
|
+
subscription options.
|
|
140
146
|
|
|
141
147
|
```typescript
|
|
142
148
|
// app.module.ts
|
|
143
|
-
import {
|
|
144
|
-
import {
|
|
149
|
+
import {Module} from '@nestjs/common';
|
|
150
|
+
import {MqttModule} from '@fetaoily/nest-mqtt';
|
|
145
151
|
|
|
146
152
|
@Module({
|
|
147
153
|
imports: [MqttModule.forRoot({
|
|
@@ -150,37 +156,40 @@ import { MqttModule } from 'nest-mqtt';
|
|
|
150
156
|
share: 'group1'
|
|
151
157
|
})]
|
|
152
158
|
})
|
|
153
|
-
export class AppModule {
|
|
159
|
+
export class AppModule {
|
|
160
|
+
}
|
|
154
161
|
```
|
|
155
162
|
|
|
156
163
|
- Configure in Subscribe
|
|
157
164
|
|
|
158
165
|
```typescript
|
|
159
|
-
import {
|
|
160
|
-
import {
|
|
166
|
+
import {Injectable} from '@nestjs/common';
|
|
167
|
+
import {Subscribe, Payload, Topic} from '@fetaoily/nest-mqtt';
|
|
161
168
|
|
|
162
169
|
@Injectable()
|
|
163
170
|
export class TestService {
|
|
164
171
|
@Subscribe('test')
|
|
165
172
|
test() {
|
|
166
|
-
|
|
173
|
+
|
|
167
174
|
}
|
|
168
|
-
|
|
175
|
+
|
|
169
176
|
@Subscribe({
|
|
170
177
|
topic: 'test2',
|
|
171
178
|
queue: true,
|
|
172
179
|
})
|
|
173
180
|
test2() {
|
|
174
|
-
|
|
181
|
+
|
|
175
182
|
}
|
|
176
183
|
}
|
|
177
184
|
```
|
|
178
185
|
|
|
179
|
-
The priority of subscribe is higher than the global mode. If you want to specify a topic do not use the shared mode, set
|
|
186
|
+
The priority of subscribe is higher than the global mode. If you want to specify a topic do not use the shared mode, set
|
|
187
|
+
it as false in subscribe decorator.
|
|
180
188
|
|
|
181
189
|
## Support
|
|
182
190
|
|
|
183
|
-
Nest-mqtt is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers.
|
|
191
|
+
Nest-mqtt is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers.
|
|
192
|
+
If you'd like to join them, please [read more here](https://docs.nestjs.com/support).
|
|
184
193
|
|
|
185
194
|
## Stay in touch
|
|
186
195
|
|