Skip to content

Commit 06d0f12

Browse files
feature(microservices) add a few options to the nats strategy
1 parent 1eef513 commit 06d0f12

3 files changed

Lines changed: 25 additions & 9 deletions

File tree

packages/microservices/interfaces/microservice-configuration.interface.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export interface GrpcOptions {
3737
oneofs?: boolean;
3838
json?: boolean;
3939
includeDirs?: string[];
40-
}
40+
};
4141
};
4242
}
4343

@@ -76,6 +76,9 @@ export interface NatsOptions {
7676
maxReconnectAttempts?: number;
7777
reconnectTimeWait?: number;
7878
servers?: string[];
79+
reconnect?: boolean;
80+
pedantic?: boolean;
7981
tls?: any;
82+
queue?: string;
8083
};
8184
}

packages/microservices/server/server-nats.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,22 @@ export class ServerNats extends Server implements CustomTransportStrategy {
4040
}
4141

4242
public bindEvents(client: Client) {
43-
const registeredPatterns = Object.keys(this.messageHandlers);
44-
registeredPatterns.forEach(channel => {
43+
const queue = this.getOptionsProp<NatsOptions>(this.options, 'queue');
44+
const subscribe = (channel: string) => {
45+
if (queue) {
46+
return client.subscribe(
47+
channel,
48+
{ queue },
49+
this.getMessageHandler(channel, client).bind(this),
50+
);
51+
}
4552
client.subscribe(
4653
channel,
4754
this.getMessageHandler(channel, client).bind(this),
4855
);
49-
});
56+
};
57+
const registeredPatterns = Object.keys(this.messageHandlers);
58+
registeredPatterns.forEach(channel => subscribe(channel));
5059
}
5160

5261
public close() {
@@ -64,7 +73,8 @@ export class ServerNats extends Server implements CustomTransportStrategy {
6473
}
6574

6675
public getMessageHandler(channel: string, client: Client) {
67-
return async (buffer, replyTo: string) => this.handleMessage(channel, buffer, client, replyTo);
76+
return async (buffer, replyTo: string) =>
77+
this.handleMessage(channel, buffer, client, replyTo);
6878
}
6979

7080
public async handleMessage(
@@ -88,9 +98,12 @@ export class ServerNats extends Server implements CustomTransportStrategy {
8898

8999
public getPublisher(publisher: Client, replyTo: string, id: string) {
90100
return response =>
91-
publisher.publish(replyTo, Object.assign(response, {
92-
id,
93-
}) as any);
101+
publisher.publish(
102+
replyTo,
103+
Object.assign(response, {
104+
id,
105+
}),
106+
);
94107
}
95108

96109
public handleError(stream) {

sample/03-microservices/src/common/strategies/nats.strategy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class NatsStrategy extends ServerNats {
1818
handlers.forEach(({ key, value }) =>
1919
client.subscribe(
2020
value.pattern,
21-
value.queue,
21+
{ queue: value.queue },
2222
this.getMessageHandler(key, client).bind(this),
2323
),
2424
);

0 commit comments

Comments
 (0)