Skip to content

Commit abea7a2

Browse files
Merge branch 'docs-dynamic-config-microserivces' of github.com:raeisimv/docs.nestjs.com into raeisimv-docs-dynamic-config-microserivces
2 parents 7177ebb + 9b6d14b commit abea7a2

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

content/microservices/basics.md

+30
Original file line numberDiff line numberDiff line change
@@ -535,3 +535,33 @@ You can also pass an array of CAs if your setup involves multiple trusted author
535535
Once everything is set up, you can inject the `ClientProxy` as usual using the `@Inject()` decorator to use the client in your services. This ensures encrypted communication across your NestJS microservices, with Node's `TLS` module handling the encryption details.
536536

537537
For more information, refer to Node’s [TLS documentation](https://nodejs.org/api/tls.html).
538+
539+
540+
#### Dynamic Configuration
541+
542+
In scenarios where the microservice needs to be configured using the `ConfigService`, but the injection context becomes available only after the microservice instance is created, the `AsyncMicroserviceOptions` provides a solution. This approach enables dynamic configuration of the microservice, ensuring seamless integration with the `ConfigService`.
543+
544+
```typescript
545+
import { ConfigService } from '@nestjs/config';
546+
import { AsyncMicroserviceOptions, Transport } from '@nestjs/microservices';
547+
import { AppModule } from './app.module';
548+
549+
async function bootstrap() {
550+
const app = await NestFactory.createMicroservice<AsyncMicroserviceOptions>(
551+
AppModule,
552+
{
553+
useFactory: (confService: ConfigService) => ({
554+
transport: Transport.TCP,
555+
options: {
556+
host: confService.get<string>('HOST'),
557+
port: confService.get<number>('PORT'),
558+
},
559+
}),
560+
inject: [ConfigService],
561+
}
562+
);
563+
564+
await app.listen();
565+
}
566+
bootstrap();
567+
```

0 commit comments

Comments
 (0)