Code Monkey home page Code Monkey logo

nestjs-monorepo-tcp-microservice's Introduction

nestjs-monorepo-tcp-microservice


🧪 Lab repo for test/implement NestJS native microservice with TCP communication. I will use monorepo mode to put client and server in the same project.

¿How to setting-up monorepo in Nest?


  • nest new project_name -> This create a standard HTTP aplication.
  • cd project_name
  • nest generate app app_name -> This converts the "normal" app workspace to a monorepo workspace. We can see a new directory called /apps, inside there we find the project_name HTTP app and the new app_name recently created.

Configuring TCP microservice


Requirements:

  • @nestjs/microservices dependency installed

main.ts

Here we create a microservice using createMicroservice method of NestFactory

const microserviceOptions: MicroserviceOptions = {
transport: Transport.TCP,
options: {
host: '127.0.0.1',
port: 3001,
},
};
const app = await NestFactory.createMicroservice(MicroserviceTcpModule, microserviceOptions);
await app.listen();

Controller

We use @MessagePattern to indicate request-response paradigm. In the below code, the getRooster() listens for messages that fulfill the 'getRooster' message pattern.

  @MessagePattern('getRooster')
  getRooster(): string {
    return this.microserviceTcpService.getRooster();
  }

Configuring TCP client


The only thing we need to set is the ClientProxy in the service . This proxy allow us to communicate the client with the microservice.

  private client: ClientProxy;

  constructor() {
    this.client = ClientProxyFactory.create({
      transport: Transport.TCP,
      options: {
        host: '127.0.0.1', // microservice host
        port: 3001, // microservice port
      }
    });
  }

Once the communication is established we can execute send method to make the request.

/*
 The second parameter is empty string because getRooster doesn't receive any parameter, 
 but is mandatory in send method indicate one.
 */
return this.client.send('getRooster', '');

nestjs-monorepo-tcp-microservice's People

Contributors

galloafranco avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.