Skip to content

Commit 7347189

Browse files
committed
docs(microservices): document custom msg options
1 parent 107dcb2 commit 7347189

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed

content/microservices/mqtt.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,62 @@ getTemperature(context) {
115115
console.log(`Topic: ${context.getTopic()}`);
116116
}
117117
```
118+
119+
#### Message options
120+
121+
It's possible to customize the message sent to the MQTT server to e.g.: adjust the QoS level, set the Retain or DUP flags, or add additional properties to the payload
122+
123+
```typescript
124+
import { MqttRecordBuilder } from '@nestjs/microservices';
125+
126+
const userProperties = { 'x-version': '1.0.0' };
127+
const record = new MqttRecordBuilder(':cat:')
128+
.setProperties({ userProperties })
129+
.setQoS(1)
130+
.build();
131+
client.send('replace-emoji', record);
132+
```
133+
134+
And you can read those options server-side as well, by accessing the MqttContext
135+
136+
```typescript
137+
@@filename()
138+
@MessagePattern('replace-emoji')
139+
replaceEmoji(@Payload() data: string, @Ctx() context: MqttContext): string {
140+
const { properties: { userProperties } } = context.getPacket();
141+
return userProperties['x-version'] === '1.0.0' ? '🐱' : '🐈';
142+
}
143+
@@switch
144+
@Bind(Payload(), Ctx())
145+
@MessagePattern('replace-emoji')
146+
replaceEmoji(data, context) {
147+
const { properties: { userProperties } } = context.getPacket();
148+
return userProperties['x-version'] === '1.0.0' ? '🐱' : '🐈';
149+
}
150+
```
151+
152+
In some cases you might want to configure user properties for multiple requests, you can pass these as options to the ClientProxyFactory
153+
154+
```typescript
155+
import { Module } from '@nestjs/common';
156+
import { ClientProxyFactory, Transport } from '@nestjs/microservices';
157+
158+
@Module({
159+
providers: [
160+
{
161+
provide: 'API_v1',
162+
useFactory: () =>
163+
ClientProxyFactory.create({
164+
transport: Transport.MQTT,
165+
options: {
166+
url: 'mqtt://localhost:1833',
167+
userProperties: { 'x-version': '1.0.0' },
168+
},
169+
}),
170+
},
171+
],
172+
})
173+
export class ApiModule {}
174+
```
175+
176+
> info **Hint** You could make this provider request-scoped and thus enable things like cross-protocol request tracing.

content/microservices/nats.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,61 @@ getDate(data, context) {
125125
return new Date().toLocaleTimeString(...);
126126
}
127127
```
128+
129+
#### Headers
130+
131+
It's possible to send headers with both request-response and event-based flows.
132+
133+
```typescript
134+
import nats from 'nats';
135+
import { NatsRecordBuilder } from '@nestjs/microservices';
136+
137+
const headers = nats.headers();
138+
headers.set('x-version', '1.0.0');
139+
const record = new NatsRecordBuilder(':cat:').setHeaders(headers).build();
140+
client.send('replace-emoji', record);
141+
```
142+
143+
And you can read those headers server-side as well, by accessing the NatsContext
144+
145+
```typescript
146+
@@filename()
147+
@MessagePattern('replace-emoji')
148+
replaceEmoji(@Payload() data: string, @Ctx() context: NatsContext): string {
149+
const headers = context.getHeaders();
150+
return headers['x-version'] === '1.0.0' ? '🐱' : '🐈';
151+
}
152+
@@switch
153+
@Bind(Payload(), Ctx())
154+
@MessagePattern('replace-emoji')
155+
replaceEmoji(data, context) {
156+
const headers = context.getHeaders();
157+
return headers['x-version'] === '1.0.0' ? '🐱' : '🐈';
158+
}
159+
```
160+
161+
In some cases you might want to configure headers for multiple requests, you can pass these as options to the ClientProxyFactory
162+
163+
```typescript
164+
import { Module } from '@nestjs/common';
165+
import { ClientProxyFactory, Transport } from '@nestjs/microservices';
166+
167+
@Module({
168+
providers: [
169+
{
170+
provide: 'API_v1',
171+
useFactory: () =>
172+
ClientProxyFactory.create({
173+
transport: Transport.NATS,
174+
options: {
175+
servers: ['nats://localhost:4222'],
176+
headers: { 'x-version': '1.0.0' },
177+
},
178+
}),
179+
},
180+
],
181+
})
182+
export class ApiModule {}
183+
```
184+
185+
> info **Hint** You could make this provider request-scoped and thus enable things like cross-protocol request tracing.

content/microservices/rabbitmq.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ The `options` property is specific to the chosen transporter. The <strong>Rabbit
7474
<td><code>socketOptions</code></td>
7575
<td>Additional socket options (read more <a href="https://www.squaremobius.net/amqp.node/channel_api.html#socket-options" rel="nofollow" target="_blank">here</a>)</td>
7676
</tr>
77+
<tr>
78+
<td><code>headers</code></td>
79+
<td>Headers to be sent along with every message</td>
80+
</tr>
7781
</table>
7882

7983
#### Client
@@ -195,3 +199,37 @@ getNotifications(data, context) {
195199
channel.ack(originalMsg);
196200
}
197201
```
202+
203+
#### Message options
204+
205+
It's possible to customize the message sent to the RabbitMQ server to e.g.: add headers or configure priority
206+
207+
```typescript
208+
import { RmqRecordBuilder } from '@nestjs/microservices';
209+
210+
const record = new RmqRecordBuilder(':cat:')
211+
.setOptions({
212+
headers: { 'x-version': '1.0.0' },
213+
priority: 3,
214+
})
215+
.build();
216+
client.send('replace-emoji', record);
217+
```
218+
219+
And you can read those values server-side as well, by accessing the RmqContext
220+
221+
```typescript
222+
@@filename()
223+
@MessagePattern('replace-emoji')
224+
replaceEmoji(@Payload() data: string, @Ctx() context: RmqContext): string {
225+
const { properties: { headers } } = context.getMessage();
226+
return headers['x-version'] === '1.0.0' ? '🐱' : '🐈';
227+
}
228+
@@switch
229+
@Bind(Payload(), Ctx())
230+
@MessagePattern('replace-emoji')
231+
replaceEmoji(data, context) {
232+
const { properties: { headers } } = context.getMessage();
233+
return headers['x-version'] === '1.0.0' ? '🐱' : '🐈';
234+
}
235+
```

0 commit comments

Comments
 (0)