You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/microservices/basics.md
+64Lines changed: 64 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -410,3 +410,67 @@ this.client
410
410
> info **Hint** The `timeout` operator is imported from the `rxjs/operators` package.
411
411
412
412
After 5 seconds, if the microservice isn't responding, it will throw an error.
413
+
414
+
#### TLS support
415
+
416
+
WWhen communicating outside of a private network, it’s important to encrypt traffic to ensure security. In NestJS, this can be achieved with TLS over TCP using Node's built-in [TLS](https://nodejs.org/api/tls.html) module. Nest provides built-in support for TLS in its TCP transport, allowing us to encrypt communication between microservices or clients.
417
+
418
+
To enable TLS for a TCP server, you'll need both a private key and a certificate in PEM format. These are added to the server's options by setting the `tlsOptions` and specifying the key and cert files, as shown below:
419
+
420
+
```typescript
421
+
import*asfsfrom'fs';
422
+
import { NestFactory } from'@nestjs/core';
423
+
import { AppModule } from'./app.module';
424
+
import { MicroserviceOptions, Transport } from'@nestjs/microservices';
For a client to communicate securely over TLS, we also define the `tlsOptions` object but this time with the CA certificate. This is the certificate of the authority that signed the server's certificate. This ensures that the client trusts the server's certificate and can establish a secure connection.
449
+
450
+
```typescript
451
+
import { Module } from'@nestjs/common';
452
+
import { ClientsModule, Transport } from'@nestjs/microservices';
You can also pass an array of CAs if your setup involves multiple trusted authorities.
473
+
474
+
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.
475
+
476
+
For more information, refer to Node’s [TLS documentation](https://nodejs.org/api/tls.html).
0 commit comments