Skip to content

Commit f6c177a

Browse files
Merge branch 'tuxmachine-feat/msvc-header-docs'
2 parents 32903e2 + a7e86d8 commit f6c177a

File tree

4 files changed

+127
-15
lines changed

4 files changed

+127
-15
lines changed

content/microservices/mqtt.md

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,59 @@ getTemperature(context) {
118118

119119
#### Record builders
120120

121-
To configure message options, you can use the `MqttRecordBuilder` class. For example, to set `QoS` to `2` use the `setQoS` method, as follows:
121+
To configure message options (adjust the QoS level, set the Retain or DUP flags, or add additional properties to the payload), you can use the `MqttRecordBuilder` class. For example, to set `QoS` to `2` use the `setQoS` method, as follows:
122122

123123
```typescript
124-
const message = { event: 'USER_CREATED' };
125-
const record = new MqttRecordBuilder(message).setQoS(2).build();
126-
this.client.send('notifications', record).subscribe(...);
124+
const userProperties = { 'x-version': '1.0.0' };
125+
const record = new MqttRecordBuilder(':cat:')
126+
.setProperties({ userProperties })
127+
.setQoS(1)
128+
.build();
129+
client.send('replace-emoji', record).subscribe(...);
127130
```
128131

129132
> info **Hint** `MqttRecordBuilder` class is exported from the `@nestjs/microservices` package.
133+
134+
And you can read these options on the 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 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: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ getDate(data, context) {
128128

129129
#### Record builders
130130

131-
To configure message options, you can use the `NatsRecordBuilder` class. For example, to add `x-version` header, use the `setHeaders` method, as follows:
131+
To configure message options, you can use the `NatsRecordBuilder` class (note: this is doable for event-based flows as well). For example, to add `x-version` header, use the `setHeaders` method, as follows:
132132

133133
```typescript
134134
import * as nats from 'nats';
@@ -137,9 +137,52 @@ import * as nats from 'nats';
137137
const headers = nats.headers();
138138
headers.set('x-version', '1.0.0');
139139

140-
const message = { event: 'USER_CREATED' };
141-
const record = new NatsRecordBuilder(message).setHeaders(headers).build();
142-
this.client.send('notifications', record).subscribe(...);
140+
const record = new NatsRecordBuilder(':cat:').setHeaders(headers).build();
141+
this.client.send('replace-emoji', record).subscribe(...);
143142
```
144143

145144
> info **Hint** `NatsRecordBuilder` class is exported from the `@nestjs/microservices` package.
145+
146+
And you can read these headers on the server-side as well, by accessing the `NatsContext`, as follows:
147+
148+
```typescript
149+
@@filename()
150+
@MessagePattern('replace-emoji')
151+
replaceEmoji(@Payload() data: string, @Ctx() context: NatsContext): string {
152+
const headers = context.getHeaders();
153+
return headers['x-version'] === '1.0.0' ? '🐱' : '🐈';
154+
}
155+
@@switch
156+
@Bind(Payload(), Ctx())
157+
@MessagePattern('replace-emoji')
158+
replaceEmoji(data, context) {
159+
const headers = context.getHeaders();
160+
return headers['x-version'] === '1.0.0' ? '🐱' : '🐈';
161+
}
162+
```
163+
164+
In some cases you might want to configure headers for multiple requests, you can pass these as options to the `ClientProxyFactory`:
165+
166+
```typescript
167+
import { Module } from '@nestjs/common';
168+
import { ClientProxyFactory, Transport } from '@nestjs/microservices';
169+
170+
@Module({
171+
providers: [
172+
{
173+
provide: 'API_v1',
174+
useFactory: () =>
175+
ClientProxyFactory.create({
176+
transport: Transport.NATS,
177+
options: {
178+
servers: ['nats://localhost:4222'],
179+
headers: { 'x-version': '1.0.0' },
180+
},
181+
}),
182+
},
183+
],
184+
})
185+
export class ApiModule {}
186+
```
187+
188+
> info **Hint** You could make this provider request-scoped and thus enable things like cross-protocol request tracing.

content/microservices/rabbitmq.md

Lines changed: 25 additions & 3 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
@@ -198,10 +202,10 @@ getNotifications(data, context) {
198202

199203
#### Record builders
200204

201-
To configure message options, you can use the `RmqRecordBuilder` class. For example, to set `headers` and `priority` properties, use the `setOptions` method, as follows:
205+
To configure message options, you can use the `RmqRecordBuilder` class (note: this is doable for event-based flows as well). For example, to set `headers` and `priority` properties, use the `setOptions` method, as follows:
202206

203207
```typescript
204-
const message = { event: 'USER_CREATED' };
208+
const message = ':cat:';
205209
const record = new RmqRecordBuilder(message)
206210
.setOptions({
207211
headers: {
@@ -211,7 +215,25 @@ const record = new RmqRecordBuilder(message)
211215
})
212216
.build();
213217

214-
this.client.send('notifications', record).subscribe(...);
218+
this.client.send('replace-emoji', record).subscribe(...);
215219
```
216220

217221
> info **Hint** `RmqRecordBuilder` class is exported from the `@nestjs/microservices` package.
222+
223+
And you can read these values on the server-side as well, by accessing the `RmqContext`, as follows:
224+
225+
```typescript
226+
@@filename()
227+
@MessagePattern('replace-emoji')
228+
replaceEmoji(@Payload() data: string, @Ctx() context: RmqContext): string {
229+
const { properties: { headers } } = context.getMessage();
230+
return headers['x-version'] === '1.0.0' ? '🐱' : '🐈';
231+
}
232+
@@switch
233+
@Bind(Payload(), Ctx())
234+
@MessagePattern('replace-emoji')
235+
replaceEmoji(data, context) {
236+
const { properties: { headers } } = context.getMessage();
237+
return headers['x-version'] === '1.0.0' ? '🐱' : '🐈';
238+
}
239+
```

src/app/homepage/pages/microservices/custom-transport/custom-transport.component.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ <h4 appAnchor id="message-serialization"><span>Message serialization</span></h4>
197197
<p>If you need to add some custom logic around the serialization of responses on the client side, you can use a custom class that extends the <code>ClientProxy</code> class or one of its child classes. For modifying successful requests you can override the <code>serializeResponse</code> method, and for modifying any errors that go through this client you can override the <code>serializeError</code> method. To make use of this custom class, you can pass the class itself to the <code>ClientsModule.register()</code> method using the <code>customClass</code> property. Below is an example of a custom <code>ClientProxy</code> that serializes each error into an <code>RpcException</code>.</p>
198198

199199
<span class="filename">
200-
{{ 'error-handling.proxy' | extension: appef748bdb0474f8314c0d2d844a046dd0b313d5bd.isJsActive }}
201-
<app-tabs #appef748bdb0474f8314c0d2d844a046dd0b313d5bd></app-tabs>
200+
{{ 'error-handling.proxy' | extension: app37591e0f00090be59cdd04c6c4aa7f47d21ed33c.isJsActive }}
201+
<app-tabs #app37591e0f00090be59cdd04c6c4aa7f47d21ed33c></app-tabs>
202202
</span><pre><code class="language-typescript">
203203
import &#123; ClientTcp, RpcException &#125; from &#39;@nestjs/microservices&#39;;
204204

@@ -210,8 +210,8 @@ <h4 appAnchor id="message-serialization"><span>Message serialization</span></h4>
210210
</code></pre><p>and then use it in the <code>ClientsModule</code> like so:</p>
211211

212212
<span class="filename">
213-
{{ 'app.module' | extension: appe71a4ea7740a397e20f1e653d6c83fd05f372e95.isJsActive }}
214-
<app-tabs #appe71a4ea7740a397e20f1e653d6c83fd05f372e95></app-tabs>
213+
{{ 'app.module' | extension: app8e7d939eefa181bf7634362268838b383adc08c2.isJsActive }}
214+
<app-tabs #app8e7d939eefa181bf7634362268838b383adc08c2></app-tabs>
215215
</span><pre><code class="language-typescript">
216216
@Module(&#123;
217217
imports: [

0 commit comments

Comments
 (0)