Hybrid application

A hybrid application is one that both listens for HTTP requests, as well as makes use of connected microservices. The INestApplication instance can be connected with INestMicroservice instances through the connectMicroservice() method.

  1. const app = await NestFactory.create(AppModule);
  2. const microservice = app.connectMicroservice({
  3. transport: Transport.TCP,
  4. });
  5. await app.startAllMicroservicesAsync();
  6. await app.listen(3001);

To connect multiple microservice instances, issue the call to connectMicroservice() for each microservice:

  1. const app = await NestFactory.create(AppModule);
  2. // microservice #1
  3. const microserviceTcp = app.connectMicroservice<MicroserviceOptions>({
  4. transport: Transport.TCP,
  5. options: {
  6. port: 3001,
  7. },
  8. });
  9. // microservice #2
  10. const microserviceRedis = app.connectMicroservice<MicroserviceOptions>({
  11. transport: Transport.REDIS,
  12. options: {
  13. url: 'redis://localhost:6379',
  14. },
  15. });
  16. await app.startAllMicroservicesAsync();
  17. await app.listen(3001);